@x-sls/google-auth 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-sls/google-auth",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "src/index.js",
5
5
  "scripts": {},
6
6
  "dependencies": {
package/src/client.js CHANGED
@@ -1,63 +1,60 @@
1
- function createClient({
1
+ const createAuthUrl = ({ GOOGLE_CLIENT_ID, REDIRECT_URI, state = {} }) =>
2
+ `https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=profile+email&state=${encodeURIComponent(JSON.stringify(state))}`
3
+
4
+ const getAccessToken = async ({
2
5
  GOOGLE_CLIENT_ID,
3
6
  GOOGLE_CLIENT_SECRET,
4
- REDIRECT_URI
5
- }) {
6
- const createAuthUrl = () =>
7
- `https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=profile email`
8
-
9
- const getAccessToken = async ({ code }) => {
10
- let text
11
- try {
12
- const res = await fetch('https://oauth2.googleapis.com/token', {
13
- method: 'POST',
14
- headers: {
15
- 'Content-Type': 'application/json'
16
- },
17
- body: JSON.stringify({
18
- client_id: GOOGLE_CLIENT_ID,
19
- client_secret: GOOGLE_CLIENT_SECRET,
20
- code,
21
- redirect_uri: REDIRECT_URI,
22
- grant_type: 'authorization_code'
23
- })
7
+ REDIRECT_URI,
8
+ code
9
+ }) => {
10
+ let text
11
+ try {
12
+ const res = await fetch('https://oauth2.googleapis.com/token', {
13
+ method: 'POST',
14
+ headers: {
15
+ 'Content-Type': 'application/json'
16
+ },
17
+ body: JSON.stringify({
18
+ client_id: GOOGLE_CLIENT_ID,
19
+ client_secret: GOOGLE_CLIENT_SECRET,
20
+ code,
21
+ redirect_uri: REDIRECT_URI,
22
+ grant_type: 'authorization_code'
24
23
  })
24
+ })
25
25
 
26
- text = await res.text()
26
+ text = await res.text()
27
27
 
28
- return JSON.parse(text)
29
- } catch (error) {
30
- if (text) {
31
- console.error({ text })
32
- }
33
- throw error
28
+ return JSON.parse(text)
29
+ } catch (error) {
30
+ if (text) {
31
+ console.error({ text })
34
32
  }
33
+ throw error
35
34
  }
35
+ }
36
36
 
37
- const getUserInfo = async ({ access_token }) => {
38
- let text
39
- try {
40
- const res = await fetch('https://www.googleapis.com/oauth2/v1/userinfo', {
41
- headers: {
42
- Authorization: `Bearer ${access_token}`
43
- }
44
- })
45
-
46
- text = await res.text()
47
- return JSON.parse(text)
48
- } catch (error) {
49
- if (text) {
50
- console.error({ text })
37
+ const getUserInfo = async ({ access_token }) => {
38
+ let text
39
+ try {
40
+ const res = await fetch('https://www.googleapis.com/oauth2/v1/userinfo', {
41
+ headers: {
42
+ Authorization: `Bearer ${access_token}`
51
43
  }
52
- throw error
53
- }
54
- }
44
+ })
55
45
 
56
- return {
57
- getUserInfo,
58
- getAccessToken,
59
- createAuthUrl
46
+ text = await res.text()
47
+ return JSON.parse(text)
48
+ } catch (error) {
49
+ if (text) {
50
+ console.error({ text })
51
+ }
52
+ throw error
60
53
  }
61
54
  }
62
55
 
63
- module.exports = createClient
56
+ module.exports = {
57
+ createAuthUrl,
58
+ getAccessToken,
59
+ getUserInfo
60
+ }
package/src/handlers.js CHANGED
@@ -1,82 +1,103 @@
1
1
  const jwt = require('jsonwebtoken')
2
+ const { createAuthUrl, getAccessToken, getUserInfo } = require('./client')
2
3
 
3
- const { createAuthUrl, getAccessToken, getUserInfo } = require('./index')
4
+ function initiate({ GOOGLE_CLIENT_ID, REDIRECT_URI }) {
5
+ return event => {
6
+ const { returnUrl } = event?.queryStringParameters || {}
4
7
 
5
- async function initiate() {
6
- return {
7
- statusCode: 302,
8
- headers: {
9
- Location: createAuthUrl({ GOOGLE_CLIENT_ID, REDIRECT_URI })
8
+ const state = { returnUrl }
9
+ return {
10
+ statusCode: 302,
11
+ headers: {
12
+ Location: createAuthUrl({
13
+ GOOGLE_CLIENT_ID,
14
+ REDIRECT_URI,
15
+ state
16
+ })
17
+ }
10
18
  }
11
19
  }
12
20
  }
13
21
 
14
- async function callback(event) {
15
- const { code } = event.queryStringParameters
16
- const headers = {
17
- 'Access-Control-Allow-Origin': '*',
18
- 'Access-Control-Allow-Credentials': true,
19
- 'Access-Control-Allow-Headers': 'Content-Type, Authorization'
20
- }
22
+ function callback({
23
+ GOOGLE_CLIENT_ID,
24
+ GOOGLE_CLIENT_SECRET,
25
+ REDIRECT_URI,
26
+ JWT_SECRET
27
+ }) {
28
+ return async event => {
29
+ const { code, state } = event.queryStringParameters
21
30
 
22
- try {
23
- const { access_token } = await getAccessToken({
24
- code,
25
- GOOGLE_CLIENT_ID,
26
- GOOGLE_CLIENT_SECRET,
27
- REDIRECT_URI
28
- })
29
- const info = await getUserInfo({ access_token })
31
+ let returnUrl
32
+ try {
33
+ const data = JSON.parse(state)
34
+ returnUrl = data.returnUrl
35
+ } catch (error) {
36
+ console.error('Invalid state:', state)
37
+ returnUrl = '/'
38
+ }
30
39
 
31
- const { email, name, picture } = info
32
- const sessionToken = jwt.sign(
33
- {
34
- email,
35
- name,
36
- picture
37
- },
38
- JWT_SECRET,
39
- { expiresIn: '1d' }
40
- )
40
+ try {
41
+ const { access_token } = await getAccessToken({
42
+ code,
43
+ GOOGLE_CLIENT_ID,
44
+ GOOGLE_CLIENT_SECRET,
45
+ REDIRECT_URI
46
+ })
47
+ const info = await getUserInfo({ access_token })
41
48
 
42
- console.log({ sessionToken })
49
+ const { email, name, picture } = info
50
+ const sessionToken = jwt.sign(
51
+ {
52
+ email,
53
+ name,
54
+ picture
55
+ },
56
+ JWT_SECRET,
57
+ { expiresIn: '1d' }
58
+ )
43
59
 
44
- return {
45
- statusCode: 200,
46
- headers,
47
- body: JSON.stringify({ token: sessionToken })
48
- }
49
- } catch (error) {
50
- console.error('Error:', error)
51
- return {
52
- statusCode: 401,
53
- headers,
54
- body: JSON.stringify({ error: 'Authentication failed' })
60
+ return {
61
+ statusCode: 302,
62
+ headers: {
63
+ Location: `${returnUrl}#token=${sessionToken}`
64
+ }
65
+ }
66
+ } catch (error) {
67
+ console.error('Error:', error)
68
+ return {
69
+ statusCode: 302,
70
+ headers: {
71
+ Location: `${returnUrl}#authError=${error}`
72
+ }
73
+ }
55
74
  }
56
75
  }
57
76
  }
58
77
 
59
- async function verify(event) {
60
- try {
61
- const { token } = JSON.parse(event.body)
62
- const decoded = jwt.verify(token, JWT_SECRET)
78
+ function verify({ JWT_SECRET }) {
79
+ return async event => {
80
+ try {
81
+ const { token } = JSON.parse(event.body)
82
+ const decoded = jwt.verify(token, JWT_SECRET)
63
83
 
64
- return {
65
- statusCode: 200,
66
- headers: {
67
- 'Access-Control-Allow-Origin': '*',
68
- 'Access-Control-Allow-Credentials': true
69
- },
70
- body: JSON.stringify({ user: decoded })
71
- }
72
- } catch (error) {
73
- return {
74
- statusCode: 401,
75
- headers: {
76
- 'Access-Control-Allow-Origin': '*',
77
- 'Access-Control-Allow-Credentials': true
78
- },
79
- body: JSON.stringify({ error: 'Invalid token' })
84
+ return {
85
+ statusCode: 200,
86
+ headers: {
87
+ 'Access-Control-Allow-Origin': '*',
88
+ 'Access-Control-Allow-Credentials': true
89
+ },
90
+ body: JSON.stringify({ user: decoded })
91
+ }
92
+ } catch (error) {
93
+ return {
94
+ statusCode: 401,
95
+ headers: {
96
+ 'Access-Control-Allow-Origin': '*',
97
+ 'Access-Control-Allow-Credentials': true
98
+ },
99
+ body: JSON.stringify({ error: 'Invalid token' })
100
+ }
80
101
  }
81
102
  }
82
103
  }
package/src/index.js CHANGED
@@ -1,7 +1,7 @@
1
- const jwt = require('jsonwebtoken')
2
1
  const createClient = require('./client')
2
+ const handlers = require('./handlers')
3
3
 
4
4
  module.exports = {
5
5
  createClient,
6
- jwt
6
+ handlers
7
7
  }