session-sync-auth-site 3.0.5 → 4.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/README.md CHANGED
@@ -9,11 +9,11 @@ You may add on more fields to the user and session tables, if you like.
9
9
  # Simple backend usage
10
10
 
11
11
  ```js
12
- const express = require('express')
13
- const app = express()
14
- const cors = require('cors')
15
- const bodyParser = require('body-parser')
12
+ import express from 'express'
13
+ import cors from 'cors'
14
+ import bodyParser from 'body-parser'
16
15
 
16
+ const app = express()
17
17
  const { authenticate, setUpSessionSyncAuthRoutes } = require('session-sync-auth-site')
18
18
 
19
19
  app.use(cors())
@@ -33,7 +33,8 @@ app.use(authenticate({
33
33
  setUpSessionSyncAuthRoutes({
34
34
  app,
35
35
  siteId,
36
- authDomain,
36
+ authFrontendBaseUrl,
37
+ authBackendBaseUrl,
37
38
  jwtSecret,
38
39
  })
39
40
  ```
@@ -75,13 +76,15 @@ app.use(authenticate({
75
76
  setUpSessionSyncAuthRoutes({
76
77
  app, // required
77
78
  siteId, // required (unless getSetupInfo provided)
78
- authDomain, // required (unless getSetupInfo provided)
79
+ authFrontendBaseUrl, // required (unless getSetupInfo provided)
80
+ authBackendBaseUrl, // required (unless getSetupInfo provided)
79
81
  jwtSecret, // required (unless getSetupInfo provided)
80
82
  getSetupInfo: req => { // useful for multi-tenancy setups
81
83
  // fetch the needed values based upon req
82
84
  return {
83
85
  siteId,
84
- authDomain,
86
+ authFrontendBaseUrl,
87
+ authBackendBaseUrl,
85
88
  jwtSecret,
86
89
  extraUserTableValues, // optional
87
90
  // Note: In a multi-tenancy setup, `extraUserTableValues` should
@@ -94,7 +97,6 @@ setUpSessionSyncAuthRoutes({
94
97
  deleteUser: async ({ id, req }) => { // optional (when absent, the appropriate rows from users and sessions are deleted)
95
98
  // delete all of user's data, including appropriate rows from users and sessions tables
96
99
  },
97
- protocol: 'https',
98
100
  paths: {
99
101
  getUser: '/get-user',
100
102
  logIn: '/log-in',
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "session-sync-auth-site",
3
- "version": "3.0.5",
3
+ "version": "4.0.1",
4
4
  "main": "src/index.js",
5
+ "module": "src/index.js",
5
6
  "repository": {
6
7
  "type": "git",
7
8
  "url": "git+https://github.com/educational-resources-and-services/session-sync-auth-site.git"
@@ -13,8 +14,9 @@
13
14
  },
14
15
  "homepage": "https://github.com/educational-resources-and-services/session-sync-auth-site#readme",
15
16
  "engines": {
16
- "node": ">=10"
17
+ "node": ">=20"
17
18
  },
19
+ "type": "module",
18
20
  "scripts": {
19
21
  "dev": "npm run go-dev -s",
20
22
  "go-dev": "concurrently -k 'node ./test/dummySite.js 3001' 'node ./test/dummySite.js 3002 dummy1' 'node ./test/dummySite.js 3003 dummy2'",
@@ -1,4 +1,4 @@
1
- const setUpConnection = require('./setUpConnection')
1
+ import setUpConnection from './setUpConnection.js'
2
2
 
3
3
  const authenticate = ({
4
4
  userTableName='users',
@@ -80,7 +80,7 @@ const authenticate = ({
80
80
  ...extraUserTableSelectValues,
81
81
  accessToken,
82
82
  },
83
- ))[0]
83
+ ))[0][0]
84
84
 
85
85
  // Convert DateTime columns into ms timestamp
86
86
  const dateTimeCols = [
@@ -98,4 +98,4 @@ const authenticate = ({
98
98
  }
99
99
  )
100
100
 
101
- module.exports = authenticate
101
+ export default authenticate
@@ -1,4 +1,4 @@
1
- const setUpConnection = require('./setUpConnection')
1
+ import setUpConnection from './setUpConnection.js'
2
2
 
3
3
  ;(async () => {
4
4
 
package/src/createUser.js CHANGED
@@ -1,21 +1,19 @@
1
- const fetch = require('node-fetch')
2
- const jwt = require('jsonwebtoken')
1
+ import jwt from 'jsonwebtoken'
3
2
 
4
3
  const createUser = async ({
5
4
  email,
6
5
  req, // either this or `setup` is required
7
- setup, // keys: siteId, authDomain, and jwtSecret
6
+ setup, // keys: siteId, authBackendBaseUrl, and jwtSecret
8
7
  }={}) => {
9
8
 
10
9
  const {
11
10
  siteId,
12
- authDomain,
11
+ authBackendBaseUrl,
13
12
  jwtSecret,
14
- protocol=(setup || req).sessionSyncAuthProtocol || `https`,
15
13
  } = setup || await req.getSessionSyncAuthSetupInfo()
16
14
 
17
15
  const createUserResult = await fetch(
18
- `${protocol}://${authDomain}/api/create-user`,
16
+ `${authBackendBaseUrl}/createUser`,
19
17
  {
20
18
  method: 'post',
21
19
  body: JSON.stringify({
@@ -41,4 +39,4 @@ const createUser = async ({
41
39
 
42
40
  }
43
41
 
44
- module.exports = createUser
42
+ export default createUser
package/src/deleteUser.js CHANGED
@@ -1,22 +1,20 @@
1
- const fetch = require('node-fetch')
2
- const jwt = require('jsonwebtoken')
1
+ import jwt from 'jsonwebtoken'
3
2
 
4
3
  const deleteUser = async ({
5
4
  id,
6
5
  mergeToUserId,
7
6
  req, // either this or `setup` is required
8
- setup, // keys: siteId, authDomain, and jwtSecret
7
+ setup, // keys: siteId, authBackendBaseUrl, and jwtSecret
9
8
  }={}) => {
10
9
 
11
10
  const {
12
11
  siteId,
13
- authDomain,
12
+ authBackendBaseUrl,
14
13
  jwtSecret,
15
- protocol=req.sessionSyncAuthProtocol || `https`,
16
14
  } = setup || await req.getSessionSyncAuthSetupInfo()
17
15
 
18
16
  const deleteUserResult = await fetch(
19
- `${protocol}://${authDomain}/api/delete-user`,
17
+ `${authBackendBaseUrl}/deleteUser`,
20
18
  {
21
19
  method: 'post',
22
20
  body: JSON.stringify({
@@ -40,4 +38,4 @@ const deleteUser = async ({
40
38
 
41
39
  }
42
40
 
43
- module.exports = deleteUser
41
+ export default deleteUser
@@ -1,12 +1,11 @@
1
- const fetch = require('node-fetch')
2
- const jwt = require('jsonwebtoken')
1
+ import jwt from 'jsonwebtoken'
3
2
 
4
3
  const getLoginLink = async ({
5
4
  email,
6
5
  redirectUrl, // defaults to req.headers.origin
7
6
  origin, // defaults to `${req.protocol}://${req.headers.host}`
8
7
  req, // either this or `setup` is required
9
- setup, // keys: siteId, authDomain, and jwtSecret
8
+ setup, // keys: siteId, authBackendBaseUrl, and jwtSecret
10
9
  }={}) => {
11
10
 
12
11
  redirectUrl = redirectUrl || req.headers.origin
@@ -14,15 +13,14 @@ const getLoginLink = async ({
14
13
 
15
14
  const {
16
15
  siteId,
17
- authDomain,
16
+ authBackendBaseUrl,
18
17
  jwtSecret,
19
- protocol=req.sessionSyncAuthProtocol || `https`,
20
18
  } = setup || await req.getSessionSyncAuthSetupInfo()
21
19
 
22
20
  const loggedInRedirectUrl = `${redirectUrl}${/\?/.test(redirectUrl) ? `&` : `?`}action=successfulLogin&origin=${encodeURIComponent(origin)}&accessToken=ACCESS_TOKEN`
23
21
 
24
22
  const getLoginLinkResult = await fetch(
25
- `${protocol}://${authDomain}/api/get-login-link`,
23
+ `${authBackendBaseUrl}/getLoginLink`,
26
24
  {
27
25
  method: 'post',
28
26
  body: JSON.stringify({
@@ -47,4 +45,4 @@ const getLoginLink = async ({
47
45
 
48
46
  }
49
47
 
50
- module.exports = getLoginLink
48
+ export default getLoginLink
package/src/index.js CHANGED
@@ -1,15 +1,6 @@
1
- const authenticate = require('./authenticate')
2
- const setUpSessionSyncAuthRoutes = require('./setUpSessionSyncAuthRoutes')
3
- const createUser = require('./createUser')
4
- const updateUserAccount = require('./updateUserAccount')
5
- const deleteUser = require('./deleteUser')
6
- const getLoginLink = require('./getLoginLink')
7
-
8
- module.exports = {
9
- authenticate,
10
- setUpSessionSyncAuthRoutes,
11
- createUser,
12
- updateUserAccount,
13
- deleteUser,
14
- getLoginLink,
15
- }
1
+ export { default as authenticate } from './authenticate.js'
2
+ export { default as setUpSessionSyncAuthRoutes } from './setUpSessionSyncAuthRoutes.js'
3
+ export { default as createUser } from './createUser.js'
4
+ export { default as updateUserAccount } from './updateUserAccount.js'
5
+ export { default as deleteUser } from './deleteUser.js'
6
+ export { default as getLoginLink } from './getLoginLink.js'
@@ -187,4 +187,4 @@ const iso6393To1 = {
187
187
  zul: 'zu'
188
188
  }
189
189
 
190
- module.exports = iso6393To1
190
+ export default iso6393To1
@@ -1,5 +1,7 @@
1
1
  ;(() => {
2
2
 
3
+ if(typeof window === 'undefined') return
4
+
3
5
  let enableSSR = false
4
6
  let defaultOrigin
5
7
  const setDefaultOrigin = origin => {
@@ -68,7 +70,7 @@
68
70
  const cancelRedirectUrl = `${addOnQueryStrConnectorChar(cancelRedirectHref)}action=canceledAccountUpdate&origin=${encodeURIComponent(origin)}${getQueryStringAddOn(extraQueryParamsForCallbacks)}`
69
71
  const updatedRedirectUrl = `${addOnQueryStrConnectorChar(updatedRedirectHref)}action=successfulAccountUpdate&origin=${encodeURIComponent(origin)}${getQueryStringAddOn(extraQueryParamsForCallbacks)}`
70
72
 
71
- const queryString = `cancelRedirectUrl=${encodeURIComponent(cancelRedirectUrl)}&updatedRedirectUrl=${encodeURIComponent(updatedRedirectUrl)}`
73
+ const queryString = `cancelRedirectUrl=${encodeURIComponent(cancelRedirectUrl)}&updatedRedirectUrl=${encodeURIComponent(updatedRedirectUrl)}&accessToken=${encodeURIComponent(getAccessToken({ origin }))}`
72
74
 
73
75
  window.location = `${origin}/update-account?${queryString}`
74
76
  }
@@ -76,7 +78,7 @@
76
78
  const getUser = async ({ origin=defaultOrigin }={}) => {
77
79
  const response = await fetch(`${origin}/get-user`, {
78
80
  headers: {
79
- 'x-access-token': getAccessToken(origin),
81
+ 'x-access-token': getAccessToken({ origin }),
80
82
  },
81
83
  })
82
84
 
@@ -86,7 +88,7 @@
86
88
  const logOut = async ({ origin=defaultOrigin, extraQueryParamsForCallbacks }={}) => {
87
89
  const redirectUrl = `${location.href.replace(/\?.*$/, '')}?action=successfulLogout&origin=${encodeURIComponent(origin)}${getQueryStringAddOn(extraQueryParamsForCallbacks)}`
88
90
  const noLoginRedirectUrl = `${location.href.replace(/\?.*$/, '')}?action=unnecessaryLogout&origin=${encodeURIComponent(origin)}${getQueryStringAddOn(extraQueryParamsForCallbacks)}`
89
- const accessToken = getAccessToken(origin)
91
+ const accessToken = getAccessToken({ origin })
90
92
 
91
93
  const queryString = `redirectUrl=${encodeURIComponent(redirectUrl)}&noLoginRedirectUrl=${encodeURIComponent(noLoginRedirectUrl)}&accessToken=${encodeURIComponent(accessToken)}`
92
94
 
@@ -1,5 +1,5 @@
1
- const mysql = require('mysql2/promise')
2
- const { ConnectionString } = require('connection-string')
1
+ import mysql from 'mysql2/promise'
2
+ import { ConnectionString } from 'connection-string'
3
3
 
4
4
  const setUpConnection = async ({
5
5
  connectionStr=`mysql://root@localhost/SessionSyncAuthSite`,
@@ -39,4 +39,4 @@ const setUpConnection = async ({
39
39
 
40
40
  }
41
41
 
42
- module.exports = setUpConnection
42
+ export default setUpConnection
@@ -1,16 +1,16 @@
1
- const jwt = require('jsonwebtoken')
1
+ import jwt from 'jsonwebtoken'
2
2
 
3
- const iso6393To1 = require('./iso639-3To1')
3
+ import iso6393To1 from './iso639-3To1.js'
4
4
 
5
5
  const setUpSessionSyncAuthRoutes = ({
6
6
  app,
7
7
  siteId,
8
- authDomain,
8
+ authFrontendBaseUrl,
9
+ authBackendBaseUrl,
9
10
  jwtSecret,
10
- getSetupInfo, // for multi-tenancy setup; sent req and must return the siteId, authDomain, and jwtSecret
11
+ getSetupInfo, // for multi-tenancy setup; sent req and must return the siteId, authFrontendBaseUrl, authBackendBaseUrl, and jwtSecret
11
12
  mergeUser,
12
13
  deleteUser,
13
- protocol='https',
14
14
  paths: {
15
15
  getUser='/get-user',
16
16
  logIn='/log-in',
@@ -21,11 +21,10 @@ const setUpSessionSyncAuthRoutes = ({
21
21
  languageColType='639-3', // OPTIONS: '639-1', '639-3', 'IETF'
22
22
  }) => {
23
23
 
24
- getSetupInfo = getSetupInfo || (async () => ({ siteId, jwtSecret, authDomain }))
24
+ getSetupInfo = getSetupInfo || (async () => ({ siteId, jwtSecret, authFrontendBaseUrl, authBackendBaseUrl }))
25
25
 
26
26
  app.use((req, res, next) => {
27
27
  req.getSessionSyncAuthSetupInfo = () => getSetupInfo(req)
28
- req.sessionSyncAuthProtocol = protocol
29
28
  next()
30
29
  })
31
30
 
@@ -38,7 +37,7 @@ const setUpSessionSyncAuthRoutes = ({
38
37
 
39
38
  app.get(logIn, async (req, res, next) => {
40
39
  const { loggedInRedirectUrl, cancelRedirectUrl } = req.query
41
- const { siteId, jwtSecret, authDomain } = await getSetupInfo(req)
40
+ const { siteId, jwtSecret, authFrontendBaseUrl } = await getSetupInfo(req)
42
41
 
43
42
  const jwtData = jwt.sign(
44
43
  {
@@ -48,27 +47,37 @@ const setUpSessionSyncAuthRoutes = ({
48
47
  jwtSecret,
49
48
  )
50
49
 
51
- res.redirect(`${protocol}://${authDomain}/login?siteId=${encodeURIComponent(siteId)}&jwtData=${encodeURIComponent(jwtData)}`)
50
+ res.redirect(`${authFrontendBaseUrl}/log-in?siteId=${encodeURIComponent(siteId)}&jwtData=${encodeURIComponent(jwtData)}`)
52
51
  })
53
52
 
54
53
  app.get(updateAccount, async (req, res, next) => {
55
54
  const { updatedRedirectUrl, cancelRedirectUrl } = req.query
56
- const { siteId, jwtSecret, authDomain } = await getSetupInfo(req)
55
+ const { siteId, jwtSecret, authFrontendBaseUrl } = await getSetupInfo(req)
56
+
57
+ if(!req.user) {
58
+ console.warn(`updateAccount attemped with no login`)
59
+ return res.redirect(cancelRedirectUrl)
60
+ }
61
+
62
+ const {
63
+ userTableColNameMap,
64
+ } = req.sessionSyncAuthSiteOptions
57
65
 
58
66
  const jwtData = jwt.sign(
59
67
  {
68
+ groupUserId: req.user[userTableColNameMap.id || `id`],
60
69
  cancelRedirectUrl,
61
70
  updatedRedirectUrl,
62
71
  },
63
72
  jwtSecret,
64
73
  )
65
74
 
66
- res.redirect(`${protocol}://${authDomain}/account?siteId=${encodeURIComponent(siteId)}&jwtData=${encodeURIComponent(jwtData)}`)
75
+ res.redirect(`${authFrontendBaseUrl}/user-account?siteId=${encodeURIComponent(siteId)}&jwtData=${encodeURIComponent(jwtData)}`)
67
76
  })
68
77
 
69
78
  app.get(logOut, async (req, res, next) => {
70
79
  const { redirectUrl, noLoginRedirectUrl } = req.query
71
- const { siteId, jwtSecret, authDomain } = await getSetupInfo(req)
80
+ const { siteId, jwtSecret, authFrontendBaseUrl } = await getSetupInfo(req)
72
81
 
73
82
  if(!req.user) {
74
83
  console.warn(`logout attemped with no login`)
@@ -87,7 +96,7 @@ const setUpSessionSyncAuthRoutes = ({
87
96
  jwtSecret,
88
97
  )
89
98
 
90
- res.redirect(`${protocol}://${authDomain}/logout?siteId=${encodeURIComponent(siteId)}&jwtData=${encodeURIComponent(jwtData)}`)
99
+ res.redirect(`${authFrontendBaseUrl}/log-out?siteId=${encodeURIComponent(siteId)}&jwtData=${encodeURIComponent(jwtData)}`)
91
100
  })
92
101
 
93
102
  app.post(authSync, async (req, res, next) => {
@@ -244,7 +253,7 @@ const setUpSessionSyncAuthRoutes = ({
244
253
  if(!id) throw "Invalid payload. Item in `payload.usersToDelete` missing `id` key."
245
254
  if(!email) throw "Invalid payload. Item in `payload.usersToDelete` missing `email` key."
246
255
 
247
- const [ userRow ] = await global.sessionSyncAuthSiteConnection.query(
256
+ const [[ userRow ]] = await global.sessionSyncAuthSiteConnection.query(
248
257
  `
249
258
  SELECT \`${(userTableColNameMap.email || `email`).replace(/`/g, '')}\` AS email
250
259
  FROM \`${userTableName}\`
@@ -299,4 +308,4 @@ const setUpSessionSyncAuthRoutes = ({
299
308
 
300
309
  }
301
310
 
302
- module.exports = setUpSessionSyncAuthRoutes
311
+ export default setUpSessionSyncAuthRoutes
@@ -1,22 +1,20 @@
1
- const fetch = require('node-fetch')
2
- const jwt = require('jsonwebtoken')
1
+ import jwt from 'jsonwebtoken'
3
2
 
4
3
  const updateUserAccount = async ({
5
4
  userId,
6
5
  data,
7
6
  req, // either this or `setup` is required
8
- setup, // keys: siteId, authDomain, and jwtSecret
7
+ setup, // keys: siteId, authBackendBaseUrl, and jwtSecret
9
8
  }={}) => {
10
9
 
11
10
  const {
12
11
  siteId,
13
- authDomain,
12
+ authBackendBaseUrl,
14
13
  jwtSecret,
15
- protocol=req.sessionSyncAuthProtocol || `https`,
16
14
  } = setup || await req.getSessionSyncAuthSetupInfo()
17
15
 
18
16
  const updateUserAccountResult = await fetch(
19
- `${protocol}://${authDomain}/api/update-user-account`,
17
+ `${authBackendBaseUrl}/updateUserAccount`,
20
18
  {
21
19
  method: 'post',
22
20
  body: JSON.stringify({
@@ -40,4 +38,4 @@ const updateUserAccount = async ({
40
38
 
41
39
  }
42
40
 
43
- module.exports = updateUserAccount
41
+ export default updateUserAccount
package/test/dummySite.js CHANGED
@@ -1,19 +1,21 @@
1
1
  'use strict'
2
2
 
3
- const express = require('express')
4
- const app = express()
5
- const cors = require('cors')
6
- const bodyParser = require('body-parser')
3
+ import express from 'express'
4
+ import cors from 'cors'
5
+ import bodyParser from 'body-parser'
6
+
7
+ import authenticate from '../src/authenticate'
8
+ import setUpSessionSyncAuthRoutes from '../src/setUpSessionSyncAuthRoutes'
7
9
 
8
- const authenticate = require('../src/authenticate')
9
- const setUpSessionSyncAuthRoutes = require('../src/setUpSessionSyncAuthRoutes')
10
+ const app = express()
10
11
 
11
12
  try {
12
13
 
13
14
  const [
14
15
  port,
15
16
  dbPrefix,
16
- authDomain,
17
+ authFrontendBaseUrl,
18
+ authBackendBaseUrl,
17
19
  x,
18
20
  ] = process.argv.slice(2)
19
21
 
@@ -56,9 +58,9 @@ try {
56
58
  setUpSessionSyncAuthRoutes({
57
59
  app,
58
60
  siteId: port,
59
- authDomain: authDomain || `localhost:3005`,
61
+ authFrontendBaseUrl: authFrontendBaseUrl || `localhost:3000`,
62
+ authBackendBaseUrl: authBackendBaseUrl || `localhost:8081`,
60
63
  jwtSecret: `secret:${port}`,
61
- protocol: authDomain ? `https` : `http`,
62
64
  languageColType: '639-1',
63
65
  })
64
66
  }
@@ -78,9 +80,9 @@ try {
78
80
  } catch(err) {
79
81
 
80
82
  const logSyntax = () => {
81
- console.log(`Syntax: \`npm run dev [port] [dbPrefix] [authDomain]\`\n`)
83
+ console.log(`Syntax: \`npm run dev [port] [dbPrefix] [authFrontendBaseUrl] [authBackendBaseUrl]\`\n`)
82
84
  console.log(`Example #1: \`npm run 3002 db1\``)
83
- console.log(`Example #2: \`npm run 3003 db2 auth.staging.resourcingeducation.com\``)
85
+ console.log(`Example #2: \`npm run 3003 db2 https://staging.resourcingeducation.com https://auth.staging.resourcingeducation.com/apis/auth\``)
84
86
  console.log(``)
85
87
  }
86
88
 
@@ -112,4 +114,4 @@ try {
112
114
 
113
115
  }
114
116
 
115
- module.exports = app
117
+ export default app