Skip to content

Commit f28359b

Browse files
committed
Add admin session minting endpoint
1 parent c0aef13 commit f28359b

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const AuthenticationController = require('../Authentication/AuthenticationController')
2+
const EmailHelper = require('../Helpers/EmailHelper')
3+
const UserGetter = require('../User/UserGetter')
4+
5+
async function createSession(req, res) {
6+
const email = EmailHelper.parseEmail(req.body?.email)
7+
if (!email) {
8+
return res.status(400).json({ error: 'invalid_email' })
9+
}
10+
11+
const user = await UserGetter.promises.getUserByAnyEmail(email)
12+
13+
if (!user) {
14+
return res.sendStatus(404)
15+
}
16+
17+
if (user.suspended) {
18+
return res.status(403).json({ error: 'account_suspended' })
19+
}
20+
21+
await AuthenticationController.promises.createSessionForUser(user, req)
22+
23+
return res.status(201).json({
24+
user: {
25+
id: user._id.toString(),
26+
email: user.email,
27+
first_name: user.first_name,
28+
last_name: user.last_name,
29+
},
30+
})
31+
}
32+
33+
module.exports = {
34+
createSession,
35+
}

services/web/app/src/Features/Authentication/AuthenticationController.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,16 @@ function _loginAsyncHandlers(req, user, anonymousAnalyticsId, isNewUser) {
663663
return (user._login_req_ip = req.ip)
664664
}
665665

666+
AuthenticationController.createSessionForUser = function (user, req, callback) {
667+
const anonymousAnalyticsId = req.session.analyticsId
668+
const isNewUser = req.session.justRegistered || false
669+
_loginAsyncHandlers(req, user, anonymousAnalyticsId, isNewUser)
670+
_afterLoginSessionSetup(req, user, callback)
671+
}
672+
666673
AuthenticationController.promises = {
667674
finishLogin: AuthenticationController._finishLoginAsync,
675+
createSessionForUser: promisify(AuthenticationController.createSessionForUser),
668676
}
669677

670678
module.exports = AuthenticationController

services/web/app/src/router.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import UserInfoController from './Features/User/UserInfoController.js'
2222
import UserController from './Features/User/UserController.js'
2323
import UserEmailsController from './Features/User/UserEmailsController.js'
2424
import UserPagesController from './Features/User/UserPagesController.js'
25+
import AdminSessionController from './Features/Admin/AdminSessionController.js'
2526
import TutorialController from './Features/Tutorial/TutorialController.js'
2627
import DocumentController from './Features/Documents/DocumentController.js'
2728
import CompileManager from './Features/Compile/CompileManager.js'
@@ -496,6 +497,12 @@ async function initialize(webRouter, privateApiRouter, publicApiRouter) {
496497
UserInfoController.getPersonalInfo
497498
)
498499

500+
privateApiRouter.post(
501+
'/api/v1/admin/session',
502+
AuthenticationController.requirePrivateApiAuth(),
503+
AdminSessionController.createSession
504+
)
505+
499506
webRouter.get(
500507
'/user/reconfirm',
501508
UserPagesController.renderReconfirmAccountPage

0 commit comments

Comments
 (0)