@x-sls/google-auth 1.0.1

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 ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@x-sls/google-auth",
3
+ "version": "1.0.1",
4
+ "main": "src/index.js",
5
+ "scripts": {},
6
+ "dependencies": {
7
+ "jsonwebtoken": "^9.0.2"
8
+ }
9
+ }
package/src/client.js ADDED
@@ -0,0 +1,63 @@
1
+ function createClient({
2
+ GOOGLE_CLIENT_ID,
3
+ 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
+ })
24
+ })
25
+
26
+ text = await res.text()
27
+
28
+ return JSON.parse(text)
29
+ } catch (error) {
30
+ if (text) {
31
+ console.error({ text })
32
+ }
33
+ throw error
34
+ }
35
+ }
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 })
51
+ }
52
+ throw error
53
+ }
54
+ }
55
+
56
+ return {
57
+ getUserInfo,
58
+ getAccessToken,
59
+ createAuthUrl
60
+ }
61
+ }
62
+
63
+ module.exports = createClient
@@ -0,0 +1,88 @@
1
+ const jwt = require('jsonwebtoken')
2
+
3
+ const { createAuthUrl, getAccessToken, getUserInfo } = require('./index')
4
+
5
+ async function initiate() {
6
+ return {
7
+ statusCode: 302,
8
+ headers: {
9
+ Location: createAuthUrl({ GOOGLE_CLIENT_ID, REDIRECT_URI })
10
+ }
11
+ }
12
+ }
13
+
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
+ }
21
+
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 })
30
+
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
+ )
41
+
42
+ console.log({ sessionToken })
43
+
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' })
55
+ }
56
+ }
57
+ }
58
+
59
+ async function verify(event) {
60
+ try {
61
+ const { token } = JSON.parse(event.body)
62
+ const decoded = jwt.verify(token, JWT_SECRET)
63
+
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' })
80
+ }
81
+ }
82
+ }
83
+
84
+ module.exports = {
85
+ initiate,
86
+ callback,
87
+ verify
88
+ }
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ const jwt = require('jsonwebtoken')
2
+ const createClient = require('./client')
3
+
4
+ module.exports = {
5
+ createClient,
6
+ jwt
7
+ }