session-sync-auth-site 0.5.6 → 0.5.8

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
@@ -88,11 +88,11 @@ setUpSessionSyncAuthRoutes({
88
88
  // typically coincide with `extraUserTableSelectValues` above.
89
89
  }
90
90
  },
91
- mergeUser: async ({ userId, mergeToUserId }) => { // optional (when absent, merge requests will succeed even though no data is merged for this site)
92
- // move all of userId's data to mergeToUserId
91
+ mergeUser: async ({ id, mergeToUserId, req }) => { // optional (when absent, merge requests will succeed even though no data is merged for this site)
92
+ // move all of user's data to mergeToUserId
93
93
  },
94
- deleteUser: async ({ id, email }) => { // optional (when absent, the appropriate rows from users and sessions are deleted)
95
- // delete all of userId's data, including appropriate rows from users and sessions tables
94
+ deleteUser: async ({ id, req }) => { // optional (when absent, the appropriate rows from users and sessions are deleted)
95
+ // delete all of user's data, including appropriate rows from users and sessions tables
96
96
  },
97
97
  protocol: 'https',
98
98
  paths: {
@@ -108,9 +108,10 @@ setUpSessionSyncAuthRoutes({
108
108
  ## Admin backend functions
109
109
 
110
110
  ```js
111
- const { createUser, getLoginLink, updateUserAccount } = require('session-sync-auth-site')
111
+ const { createUser, getLoginLink, updateUserAccount, deleteUser } = require('session-sync-auth-site')
112
112
 
113
113
  app.post(`create-user`, (req, res, next) => {
114
+ // first check that user is admin with permission to do this
114
115
  const userId = await createUser({
115
116
  email: req.body.email,
116
117
  req,
@@ -119,6 +120,7 @@ app.post(`create-user`, (req, res, next) => {
119
120
  })
120
121
 
121
122
  app.post(`get-login-link`, (req, res, next) => {
123
+ // first check that user is admin with permission to do this
122
124
  const loginLink = await getLoginLink({
123
125
  email: req.body.email,
124
126
  redirectUrl: req.body.redirectUrl, // must begin with the frontend domain (default: req.headers.origin)
@@ -129,6 +131,7 @@ app.post(`get-login-link`, (req, res, next) => {
129
131
  })
130
132
 
131
133
  app.post(`update-user-account`, (req, res, next) => {
134
+ // first check that user is admin with permission to do this
132
135
  await updateUserAccount({
133
136
  userId: req.body.userId,
134
137
  data: { // only include details being updated
@@ -144,6 +147,16 @@ app.post(`update-user-account`, (req, res, next) => {
144
147
  })
145
148
  res.send({ success: true })
146
149
  })
150
+
151
+ app.post(`delete-user`, (req, res, next) => {
152
+ // first check that user is admin with permission to do this
153
+ await deleteUser({
154
+ id: req.body.id,
155
+ mergeToUserId: req.body.mergeToUserId, // optional
156
+ req,
157
+ })
158
+ res.send({ success: true })
159
+ })
147
160
  ```
148
161
 
149
162
  # Frontend usage
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "session-sync-auth-site",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "main": "src/index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,44 @@
1
+ const fetch = require('node-fetch')
2
+ const jwt = require('jsonwebtoken')
3
+
4
+ const deleteUser = async ({
5
+ id,
6
+ mergeToUserId,
7
+ req, // either this or `setup` is required
8
+ setup, // keys: siteId, authDomain, and jwtSecret
9
+ }={}) => {
10
+
11
+ const {
12
+ siteId,
13
+ authDomain,
14
+ jwtSecret,
15
+ protocol=req.sessionSyncAuthProtocol || `https`,
16
+ } = setup || await req.getSessionSyncAuthSetupInfo()
17
+
18
+ const deleteUserResult = await fetch(
19
+ `${protocol}://${authDomain}/api/delete-user`,
20
+ {
21
+ method: 'post',
22
+ body: JSON.stringify({
23
+ siteId,
24
+ jwtData: jwt.sign(
25
+ {
26
+ id,
27
+ mergeToUserId,
28
+ data,
29
+ },
30
+ jwtSecret,
31
+ ),
32
+ }),
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ },
36
+ },
37
+ )
38
+
39
+ if(deleteUserResult.status !== 200) throw new Error(`Delete user failed: ${deleteUserResult.statusText} [${deleteUserResult.status}]`)
40
+ if(!(await deleteUserResult.json()).success) throw new Error(`Delete user failed: bad result from auth server`)
41
+
42
+ }
43
+
44
+ module.exports = deleteUser
package/src/index.js CHANGED
@@ -2,6 +2,7 @@ const authenticate = require('./authenticate')
2
2
  const setUpSessionSyncAuthRoutes = require('./setUpSessionSyncAuthRoutes')
3
3
  const createUser = require('./createUser')
4
4
  const updateUserAccount = require('./updateUserAccount')
5
+ const deleteUser = require('./deleteUser')
5
6
  const getLoginLink = require('./getLoginLink')
6
7
 
7
8
  module.exports = {
@@ -9,5 +10,6 @@ module.exports = {
9
10
  setUpSessionSyncAuthRoutes,
10
11
  createUser,
11
12
  updateUserAccount,
13
+ deleteUser,
12
14
  getLoginLink,
13
15
  }
@@ -246,7 +246,7 @@ const setUpSessionSyncAuthRoutes = ({
246
246
 
247
247
  const [ userRow ] = await global.sessionSyncAuthSiteConnection.asyncQuery(
248
248
  `
249
- SELECT email
249
+ SELECT \`${(userTableColNameMap.email || `email`).replace(/`/g, '')}\` AS email
250
250
  FROM \`${userTableName}\`
251
251
  WHERE \`${(userTableColNameMap.id || `id`).replace(/`/g, '')}\` = :userId
252
252
  `,
@@ -262,11 +262,12 @@ const setUpSessionSyncAuthRoutes = ({
262
262
  await mergeUser({
263
263
  id,
264
264
  mergeToUserId,
265
+ req,
265
266
  })
266
267
  }
267
268
 
268
269
  if(deleteUser) {
269
- await deleteUser({ id })
270
+ await deleteUser({ id, isAfterAMerge: !!mergeToUserId, req })
270
271
  } else {
271
272
  await global.sessionSyncAuthSiteConnection.asyncQuery(
272
273
  `