About Login for Facebook

Enables login via Facebook account and PW user creation.

Category Users and Access
Modules dealing with access in ProcessWire via Users, Roles or Permissions.
Release StateBeta
Close to stable, but users are advised to be cautious and test thoroughly.*
Authorryan
Module Version0.0.2
Class NameLoginFacebook
Compatibility3.0
Date AddedSeptember 22, 2017
Recommended ByNew recommendations may take up to 1 day to appear.

Instructions

This module's files should be placed in /site/modules/LoginFacebook/
How to install or uninstall modules

README

Login for Facebook

  • Module for ProcessWire 3.x (requires PW 3.0.42 or newer).
  • Enables Facebook login/authentication for PW and use of Facebook data.
  • Developed by Ryan Cramer, made for and sponsored by Michael Barón.

How it works

This module creates a new page /login-facebook/, feel free to move or rename it as needed.When a user accesses this page, it asks them to login to Facebook. If they are already logged in to Facebook, it will ask them to approve it. Upon approval, they are redirected back to your site and now logged in to ProcessWire with a user having the role “login-facebook”. If the user has not previously done this before, a new account will be created for them. The created account uses the name of their Facebook account.

Installation

  1. Unzip and copy the module’s files to /site/modules/LoginFacebook/.

  2. Copy the included login-facebook.php to your /site/templates/ folder (if not already present).

  3. Create a new app from Facebook Developers site: https://developers.facebook.com/apps/.You will need to obtain a Facebook App ID and App Secret. Facebook will also ask for some other information, such as the following:

    • Display Name: Enter whatever you'd like, i.e. “Login for my site”.

    • Namespace: I left this one blank.

    • App domains: Enter your web site’s domain name(s). Note that if “localhost” is needed,Facebook won't take it until you've completed the “Website” first (see next item).

    • Click the “Add Platform” at the bottom, and add “Website”, along with the full http URLto it. If testing on localhost, enter “http://localhost". In my case using MAMP onlocalhost, it was “http://localhost:8888”.

    • For the rest of the fields, you can leave them blank or populate them as you see fit.

  4. Configure the module in the ProcessWire admin (Modules > Site > Login Facebook). Add theFacebook App ID and App Secret you obtained in the last step, to the indicated fields.

  5. While still in the module configuration, configure the “Page where user is redirected toafter successful login”. Leave the rest of the configuration as-is with the defaults for now. You can come back later to configure and adjust things as needed, but it's a good idea to get the module functional with the defaults first.

  6. Now lets test things out. Log out of ProcessWire (if you are logged in). Then access the/login-facebook/ URL on your website where the module is installed. It should redirect to Facebook and ask you to login, or ask you to approve the request if you are already logged in. Upon login/approval, Facebook will redirect to your designated page, a new user account will be created in ProcessWire (if not already created), and the user will be logged in.

Usage

When a user is logged in from Facebook, you can access properties from their Facebook informationvia the ProcessWire API. The fields that you want to work with are configured from the module configuration screen. As an example, lets say that you wanted to output a welcome message that contains the user's first name and picture, as stored in the Facebook data:

$facebook = $modules->get('LoginFacebook');  
echo "<h2>Welcome $facebook->first_name</h2>";
if($facebook->picture_url) {
  echo "<p><img src='$facebook->picture_url'></p>";
}

If you want to retrieve all user data, use the getAll() method, which returns an associativearray of all Facebook data that you've configured for the module to retrieve.

$facebook = $modules->get('LoginFacebook');  
$data = $facebook->getAll();

In either case, any strings in the returned data is automatically entity encoded when thecurrent page’s output formatting state is enabled.

When making API calls like above, if the user is not logged-in via Facebook, then it willautomatically redirect to Facebook to log them in, and return to your page (where the API call exists) once logged in. If you would instead rather check if they are logged in via Facebook before outputting data, use the isLoggedIn() method:

$facebook = $modules->get('LoginFacebook');  
if($facebook->isLoggedIn()) {
  // user is logged in and Facebook data available
  echo "<h2>Welcome $facebook->first_name</h2>";
} else {
  // user not logged in via Facebook, ask them to login
  echo "<a href='$facebook->url'>Login with Facebook</a>";
}

This module also enables you to mirror data from Facebook to ProcessWire fields. This can addconvenience, especially in cases where the user might be able to login through either ProcessWire or through Facebook. This ensures the Facebook data is stored in their ProcessWire user profile, even when not logged in via Facebook. This is useful for fields like email and names.


Copyright 2017 by Ryan Cramer

Comments

  • Rudy Affandi

    Rudy Affandi 2 months ago 10

    Ryan, it would be swell to be able to extend this module to allow login from ProcessWire site. Essentially, using PW as a login server.

  • Chris Snoek

    Chris Snoek 2 months ago 00

    Hi Ryan,

    Im using your facebook login module, works as a charm.
    But, when I have a link provided on my facebook account, and try to login within the facebook web-view (clicking link on the facebook page on mobile).

    Im getting a 'Invalid request (no Facebook state)' error.

    Any idea how to solve this?

    Thanks in advance.

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.