MyNixOS website logo
Description

Authentication backend for Yesod using Facebook.

This package allows you to use Yesod's authentication framework with Facebook as your backend. That is, your site's users will log in to your site through Facebook. Your application need to be registered on Facebook.

This package works with both the server-side authentication flow (https://developers.facebook.com/docs/authentication/server-side/) via the Yesod.Auth.Facebook.ServerSide module and the client-side authentication (https://developers.facebook.com/docs/authentication/client-side/) via the Yesod.Auth.Facebook.ClientSide module. It's up to you to decide which one to use. The server-side code is older and as such has been through a lot more testing than the client-side code. Also, for now only the server-side code is able to work with other authentication plugins. The client-side code, however, allows you to use some features that are available only to the Facebook JS SDK (such as automatically logging your users in, see https://developers.facebook.com/blog/post/2012/05/08/how-to--improve-the-experience-for-returning-users/).

yesod-auth-fb

Build Status Hackage StackageNightly StackageLTS

Authentication backend for Yesod using Facebook

Demo

Sample code showing Facebook authentication in action:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}

import Data.Text (Text)
import Yesod
import Yesod.Auth
import Yesod.Facebook
import Yesod.Auth.Facebook.ServerSide
import Facebook (Credentials(..))

fbclientId :: Text
fbclientId = "sample_fb_client_id"

fbclientSecret :: Text
fbclientSecret = "sample_fb_secret"

data App =
  App

mkYesod
  "App"
  [parseRoutes|
/ HomeR GET
/auth AuthR Auth getAuth
|]

instance Yesod App where
  approot = ApprootStatic "http://localhost:3000"

instance YesodFacebook App where
  fbCredentials _ = Credentials "yesod" fbclientId fbclientSecret

instance YesodAuth App where
  type AuthId App = Text
  getAuthId = return . Just . credsIdent
  loginDest _ = HomeR
  logoutDest _ = HomeR
  authPlugins _ = [authFacebook ["user_about_me", "email"]]
  -- The default maybeAuthId assumes a Persistent database. We're going for a
  -- simpler AuthId, so we'll just do a direct lookup in the session.
  maybeAuthId = lookupSession "_ID"

instance RenderMessage App FormMessage where
  renderMessage _ _ = defaultFormMessage

getHomeR :: Handler Html
getHomeR = do
  maid <- maybeAuthId
  defaultLayout
    [whamlet|
            <p>Your current auth ID: #{show maid}
            $maybe _ <- maid
                <p>
                    <a href=@{AuthR LogoutR}>Logout
                    <a href=@{AuthR facebookLogout}>Facebook logout
            $nothing
                <p>
                    <a href=@{AuthR LoginR}>Go to the login page
        |]

main :: IO ()
main = warp 3000 App
Metadata

Version

1.10.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows