Skip to content

annejan/bolt-extension-clientlogin

 
 

Repository files navigation

Client Login

An extension to remember authenticated visitors on your Bolt website. This extension uses HybridAuth for the actual authentication process

Installation

To enable a provider set the value enabled: true in the configuration and replace the example provider keys with real values. (see below)

Adding Providers

Google

  1. Login to https://console.developers.google.com/project
  2. Click 'Create Project' (go to next step if using an existing one)
  3. Set a descriptive Project Name
  4. Agree to terms and services
  5. Click Create
  6. Expand 'APIs & auth' menu and select 'Credentials'
  7. Under 'OAuth' click 'Create new Client ID'
  8. Click 'Configure consent screen'
  9. Set your desired email address from the selector
  10. Set a unique 'Product Name'
  11. Click 'Save'
  12. In the 'Create Client ID' dialogue
  13. Set Application Type: Web Application
  14. Set Authorized JavaScript Origins: http://your-bolt-site.example.com
    (change the domain name to match yours)
  15. Set Authorized Redirect URI: http://your-bolt-site.example.com/oauth2/callback?provider=Google
    (change the domain name to match yours)
  16. Click 'Create Client ID'
  17. Under the 'APIs & auth' menu select 'APIs'
  18. Click the 'Google+ API' link (middle of the page at time of writing)
  19. Click the 'Enable API' button
  20. Click the 'Explore this API' link
  21. Click the 'Authorize requests using OAuth 2.0' switch (top right)
  22. On the 'Authorize requests using OAuth 2.0' dialog enable: * Authorize requests using OAuth 2.0 (Authorize requests using OAuth 2.0) * Know who you are on Google (https://www.googleapis.com/auth/plus.me) * View your email address (https://www.googleapis.com/auth/userinfo.email) * View your basic profile info (https://www.googleapis.com/auth/userinfo.profile)
  23. Add the 'Client ID' and 'Client Secret' to your config.yml

NOTE It may take as long as 10 minutes for the changes to propagate and the client ID and secret to work

Facebook

  1. Login to Facebook with the account you want to use for the site
  2. Go to https://developers.facebook.com
  3. Under the 'Apps' menu select 'Create a New App'
  4. In the 'Create a new app' dialogue set:
  • 'Display Name' set to something descriptive
  • 'Namespace' just leave blank
  • 'Is this a test version of another app?' leave as 'No'
  • 'Category' just select a category that fits your site content
  1. Click 'Create App' and enter displayed CAPTCHA
  • At this point your app is created in development mode and you will be redirected to the App Dashboard.
  1. In the left menu, select 'Settings'
  2. In the 'App Domains' field enter your sites domain name: - your-bolt-site.example.com
    (change the domain name to match yours)
  3. In the 'Contact Email' field enter your sites contact email address: - someone@your-bolt-site.example.com
    (change the address to match your site's)
  4. Click 'Add Platform' button
  5. Choose "Website" in displayed dialogue
  6. Enter your site's relevant URLs: - 'Site URL' - http://your-bolt-site.example.com - 'Mobile Site URL' - http://mobile.your-bolt-site.example.com
    (change the domain name to match yours)
  7. Click 'Save Changes'
  8. In the left menu, select 'Status & Review'
  9. Set the toggle next to "Do you want to make this app and all its live features available to the general public?" to Yes
  10. Click 'Confim' in the displayed dialogue
  11. In the left menu, select 'Dashboard'
  12. Add the 'App ID' and 'App Secret' to your config.yml file

Multiple URLs

  1. Go to https://developers.facebook.com
  2. In the left menu, select 'Settings'
  3. Select the 'Advanced' tab
  4. Scroll down to the 'Security' section of the page
  5. Add URLs to the 'Valid OAuth redirect URIs' field

GitHub

  1. Log into GitHub
  2. Go to: https://github.com/settings/applications/
  3. Click 'Register new application'
  4. Fill in the fields:
  5. Application name
  6. Homepage URL: http://your-bolt-site.example.com (change the domain name to match yours)
  7. Application description
  8. Authorization callback URL: http://your-bolt-site.example.com/oauth2/callback?provider=Github
    (change the domain name to match yours)
  9. Click 'Register application'
  10. Add the 'Client ID' and 'Client Secret' to your config.yml

Password

To use the password authentication you also need to install BoltForms.

Advanced Configuration

See the hybrid auth userguide for advanced configuration options.

Template Usage

You can use the following functions and snippets in your templates

Login Link(s)

There are two Twig function options for displaying the login links:

    {{ displaylogin() }}
    {{ displaylogin(true) }}

In the first instance, after authentication a user is redirected to the homepage.

By supplying the parameter true the user is redirected to the current page.

Logout Link

As with login, there are two options for the logout links:

    {{ displaylogout() }}
    {{ displaylogout(true) }}

In the first instance, after logging out a user is redirected to the homepage.

By supplying the parameter true the user is redirected back to the current page.

Dynamic Link

If you want the login/logout to be automatically varied based on whether a user is logged in or our, you can use:

    {{ displayauth() }}

Overriding Templates

Twig templates used by ClientLogin can be overridden in your config file.

Assuming you're using a theme directory of theme/my-site-theme/ you can:

  1. Create the directory theme/my-site-theme/extensions/clientlogin/
  2. Copy the contents of extensions/vendor/bolt/clientlogin/assets/ to theme/my-site-theme/extensions/clientlogin/
  3. In your app/config/extensions/clientlogin.bolt.yml file, set the template key to something similar too:
template:
    login: extensions/clientlogin/_login.twig
    password: extensions/clientlogin/_password.twig
    password_parent: extensions/clientlogin/password.twig
    feedback: extensions/clientlogin/_feedback.twig
    button: extensions/clientlogin/_button.twig

At this point you can customise the files in theme/my-site-theme/extensions/clientlogin/ as you need for your site.

ClientLogin in Bolt Extensions

This extension is pretty bare-bones by design. Most likely, you will use this extension in combination with another extension that builds on its functionality.

To get information about the current visitor:

if ($this->app['clientlogin.session']->doCheckLogin()) {
    // User is logged in
    if ($this->app['clientlogin.db']->getUserProfileBySession($this->app['clientlogin.session']->token)) {
        $username = $this->app['clientlogin.db']->user['username'];
        $provider = $this->app['clientlogin.db']->user['provider'];
        $providerdata = json_decode($this->app['clientlogin.db']->user['providerdata']);
    }
}

Event Handlers

If you want to hook into login/logout events, ClientLogin dispatches events to listeners on clientlogin.Login and clientlogin.Logout.

You can add the hooks and specify callback functions like so:

$this->app['dispatcher']->addListener('clientlogin.Login',  array($this, 'myLoginCallback'));
$this->app['dispatcher']->addListener('clientlogin.Logout', array($this, 'myLogoutCallback'));

Inside your callback you can get the array of user profile data for the login/logout with:

public function myLoginCallback(\Bolt\Extension\Bolt\ClientLogin\ClientLogin\Event\ClientLoginEvent $event)
{ 
    $userdata = $event->getUser();
}

About

bolt-extension-clientlogin

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.4%
  • HTML 0.6%