1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/docs/guides/google-auth-hook.md

256 lines
6.4 KiB
Markdown
Raw Normal View History

---
id: google_auth
title: Google Auth Hook
---
This part of the tutorial shows how to create a sign-in flow for users and integrate with Unleash server project. The implementation assumes that I am working in `localhost` with `4242` port.
This is a simple `index.js` server file.
2018-11-22 11:20:28 +01:00
```javascript
const unleash = require('unleash-server');
unleash.start(options).then(unleash => {
console.log(`Unleash started on http://localhost:${unleash.app.get('port')}`);
2018-11-22 11:20:28 +01:00
});
```
### Creating a web application client ID
1. Go to the credentials section in the [Google Cloud Platform Console](https://console.cloud.google.com/apis/credentials?_ga=2.77615956.-1991581217.1542834301).
2. Click **OAuth consent screen**. Type a product name. Fill in any relevant optional fields. Click **Save**.
3. Click **Create credentials > OAuth client ID**.
4. Under **Application type**, select **Web Application**.
5. Type the **Name**.
6. Under **Authorized redirect URIs** enter the following URLs, one at a time.
2018-11-22 11:20:28 +01:00
```
http://localhost:4242/api/auth/callback
```
7. Click **Create**.
8. Copy the **CLIENT ID** and **CLIENT SECRET** and save them for later use.
### Add dependencies
Add two dependencies [`@passport-next/passport`](https://www.npmjs.com/package/@passport-next/passport) and [`@passport-next/passport-google-oauth2`](https://www.npmjs.com/package/@passport-next/passport-google-oauth2) inside `index.js` file
2018-11-22 11:20:28 +01:00
```js
const unleash = require('unleash-server');
const passport = require('@passport-next/passport');
const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2')
.Strategy;
```
2018-11-22 11:20:28 +01:00
### Configure the Google strategy for use by Passport.js
OAuth 2-based strategies require a `verify` function which receives the credential (`accessToken`) for accessing the Google API on the user's behalf, along with the user's profile. The function must invoke `cb` with a user object, which will be set at `req.user` in route handlers after authentication.
2018-11-22 11:20:28 +01:00
```js
const GOOGLE_CLIENT_ID = '...';
const GOOGLE_CLIENT_SECRET = '...';
const GOOGLE_CALLBACK_URL = 'http://localhost:4242/api/auth/callback';
2018-11-22 11:20:28 +01:00
passport.use(
new GoogleOAuth2Strategy(
2018-11-22 11:20:28 +01:00
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: GOOGLE_CALLBACK_URL,
},
function(accessToken, refreshToken, profile, cb) {
// Extract the minimal profile information we need from the profile object
// and connect with Unleash to get name and email.
cb(
null,
new unleash.User({
name: profile.displayName,
email: profile.emails[0].value,
}),
);
},
),
);
```
Add `googleAdminAuth()` function and other options
2018-11-22 11:20:28 +01:00
```js
function googleAdminAuth(app) {}
let options = {
enableLegacyRoutes: false,
adminAuthentication: 'custom',
2018-11-22 11:20:28 +01:00
preRouterHook: googleAdminAuth,
};
unleash.start(options).then(instance => {
console.log(
`Unleash started on http://localhost:${instance.app.get('port')}`,
);
2018-11-22 11:20:28 +01:00
});
```
### In `googleAdminAuth` function
Configure `passport` package.
2018-11-22 11:20:28 +01:00
```js
function googleAdminAuth(app) {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
// ...
}
```
Implement a preRouter hook for `/api/admin/login`. It's neccesary for login with Google.
2018-11-22 11:20:28 +01:00
```js
function googleAdminAuth(app) {
// ...
2018-11-22 11:20:28 +01:00
app.get(
'/api/admin/login',
passport.authenticate('google', { scope: ['email'] }),
2018-11-22 11:20:28 +01:00
);
// ...
}
```
Implement a preRouter hook for `/api/auth/callback`. It's a callback when the login is executed.
2018-11-22 11:20:28 +01:00
```js
function googleAdminAuth(app) {
// ...
2018-11-22 11:20:28 +01:00
app.get(
'/api/auth/callback',
passport.authenticate('google', {
failureRedirect: '/api/admin/error-login',
}),
(req, res) => {
// Successful authentication, redirect to your app.
res.redirect('/');
2018-11-22 11:20:28 +01:00
},
);
// ...
}
```
Implement a preRouter hook for `/api/admin`.
2018-11-22 11:20:28 +01:00
```js
function googleAdminAuth(app) {
// ...
app.use('/api/admin/', (req, res, next) => {
if (req.user) {
next();
} else {
// Instruct unleash-frontend to pop-up auth dialog
return res
.status('401')
.json(
new unleash.AuthenticationRequired({
path: '/api/admin/login',
type: 'custom',
message: `You have to identify yourself in order to use Unleash. Click the button and follow the instructions.`,
2018-11-22 11:20:28 +01:00
}),
)
.end();
}
});
// ...
}
```
### The complete code
2018-11-22 11:20:28 +01:00
The `index.js` server file.
2018-11-22 11:20:28 +01:00
```js
'use strict';
const unleash = require('unleash-server');
const passport = require('@passport-next/passport');
const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2');
const GOOGLE_CLIENT_ID = '...';
const GOOGLE_CLIENT_SECRET = '...';
const GOOGLE_CALLBACK_URL = 'http://localhost:4242/api/auth/callback';
2018-11-22 11:20:28 +01:00
passport.use(
new GoogleOAuth2Strategy(
2018-11-22 11:20:28 +01:00
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: GOOGLE_CALLBACK_URL,
},
(accessToken, refreshToken, profile, cb) => {
2018-11-22 11:20:28 +01:00
cb(
null,
new unleash.User({
name: profile.displayName,
email: profile.emails[0].value,
}),
);
},
),
);
function googleAdminAuth(app) {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
2018-11-22 11:20:28 +01:00
app.get(
'/api/admin/login',
passport.authenticate('google', { scope: ['email'] }),
2018-11-22 11:20:28 +01:00
);
app.get(
'/api/auth/callback',
passport.authenticate('google', {
failureRedirect: '/api/admin/error-login',
}),
(req, res) => {
res.redirect('/');
2018-11-22 11:20:28 +01:00
},
);
app.use('/api/admin/', (req, res, next) => {
if (req.user) {
next();
} else {
return res
.status('401')
.json(
new unleash.AuthenticationRequired({
path: '/api/admin/login',
type: 'custom',
message: `You have to identify yourself in order to use Unleash. Click the button and follow the instructions.`,
2018-11-22 11:20:28 +01:00
}),
)
.end();
}
});
}
const options = {
enableLegacyRoutes: false,
adminAuthentication: 'custom',
2018-11-22 11:20:28 +01:00
preRouterHook: googleAdminAuth,
};
unleash.start(options).then(instance => {
console.log(
`Unleash started on http://localhost:${instance.app.get('port')}`,
);
2018-11-22 11:20:28 +01:00
});
```