You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
Ally is a social authentication provider for AdonisJs. It makes it super easy to authenticate users via 3rd party websites like Facebook, Twitter, Google, etc. with minimum efforts.
Below is the list of officially supported drivers whereas you are free to contribute and add more.
Facebook (facebook)
Google (google)
Twitter (twitter)
Github (github)
LinkedIn (linkedin)
Instagram (instagram)
Four Square (foursquare)
Ally is 1st party provider installed and configured to add support for social authentication.
You are required to define the configuration inside config/services.js
file. The configuration includes a Client Id, Secret and the Redirect URI.
Ally will attach an object called ally
to the request instance so that you can access the methods inside your controllers.
Let’s begin with the setup process which is incredibly simple as always.
npm i --save adonis-ally
const providers = [
// ...
'adonis-ally/providers/AllyProvider'
// ...
]
const globalMiddleware = [
// ...
'Adonis/Middleware/Ally'
// ...
]
Once the setup process has been done successfully, you are good to authenticate your users using their social profiles.
The configuration for ally is defined inside config/services.js
file. You can copy the sample configuration from github.
module.exports = {
ally: {
// Configuration for facebook
facebook: {
clientId: '',
clientSecret: '',
redirectUri: ''
},
// Configuration for github
github: {
clientId: '',
clientSecret: '',
redirectUri: ''
}
}
}
Let’s start with the basic example of Login with Facebook where we will authenticate the users using Facebook and will create their user account without a password.
Make sure you have defined the required configuration for facebook inside config/services.js file.
|
const Route = use('Route')
Route.get('facebook/login', 'LoginController.redirect')
Route.get('facebook/authenticated', 'LoginController.handleCallback')
First, we need to redirect the user to the facebook to allow our application to access their profile.
class LoginController {
* redirect (request, response) {
yield request.ally.driver('facebook').redirect()
}
}
const User = use('App/Model/User')
class LoginController {
* handleCallback (request, response) {
const fbUser = yield request.ally.driver('facebook').getUser() (1)
const searchAttr = {
email: fbUser.getEmail()
}
const newUser = {
email: fbUser.getEmail(),
avatar: fbUser.getAvatar(),
username: fbUser.getName()
}
const user = yield User.findOrCreate(searchAttr, newUser) (2)
request.auth.loginViaId(user.id) (3)
}
}
1 | The getUser method will fetch the user profile for the given provider. This method only works when the user has been redirected back to the redirectUri . |
2 | The findOrCreate is a lucid method to find a user with user details or create a new user if unable to find. |
3 | Finally we log in the user using their id . |
Below is the list of available methods exposed by Ally provider.
Returns redirect url for a given provider
yield request.ally.driver('facebook').getRedirectUrl()
Update the scopes to be used for asking permission.
yield request.ally.driver('facebook')
.scope(['public_profile', 'email', 'user_friends'])
.redirect()
Define custom fields when trying to access the user profile.
yield request.ally.driver('facebook')
.fields(['email', 'verified']) (1)
.getUser()
Make sure to access additional fields using the getOriginal method on user instance. |
Below is the list of methods to be used for fetching user profile details. All these methods are called on User Instance returned by getUser.
const user = yield request.ally.driver('facebook').getUser()
user.getName()
const user = yield request.ally.driver('facebook').getUser()
user.getEmail()
const user = yield request.ally.driver('facebook').getUser()
user.getNickname()
const user = yield request.ally.driver('facebook').getUser()
user.getAvatar()
const user = yield request.ally.driver('facebook').getUser()
user.getAccessToken()
Returns the refresh token to be used when access token has been expired. It is only returned when using OAuth2, and the provider supports access token expiry.
const user = yield request.ally.driver('facebook').getUser()
user.getRefreshToken()
Access token expiry time in milliseconds. It is only returned when using OAuth2, and the provider supports access token expiry.
const user = yield request.ally.driver('facebook').getUser()
user.getExpires()
Returns access token secret. It is only returned when using OAuth1.
Twitter is the only driver which makes use of OAuth1. |
const user = request.ally.driver('twitter').getUser()
user.getTokenSecret()
Returns the original response from the provider.
const user = request.ally.driver('twitter').getUser()
user.getOriginal()