/
index.js
51 lines (42 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Profile, Provider } from 'serverless-authentication'
function mapProfile(response) {
const overwrites = {
name: response.displayName,
email: response.emails ? response.emails[0].value : null,
picture: response.image ? response.image.url : null,
provider: 'google'
}
return new Profile(Object.assign(response, overwrites))
}
class GoogleProvider extends Provider {
signinHandler({
scope = 'profile', state, access_type = 'online', prompt
}) {
const variableOptions = { scope, state, access_type }
if (prompt) {
Object.assign(variableOptions, { prompt })
}
const options = Object.assign(variableOptions, {
signin_uri: 'https://accounts.google.com/o/oauth2/v2/auth',
response_type: 'code'
})
return super.signin(options)
}
callbackHandler(event) {
const options = {
authorization_uri: 'https://www.googleapis.com/oauth2/v4/token',
profile_uri: 'https://www.googleapis.com/plus/v1/people/me',
profileMap: mapProfile,
authorizationMethod: 'POST'
}
return super.callback(event, options, {
authorization: { grant_type: 'authorization_code' }
})
}
}
const signinHandler = (config, options) =>
new GoogleProvider(config).signinHandler(options)
const callbackHandler = (event, config) =>
new GoogleProvider(config).callbackHandler(event)
exports.signinHandler = signinHandler
exports.callbackHandler = callbackHandler