1
0
Fork 0
freesewing/sites/backend/src/controllers/users.mjs

127 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-11-03 19:56:06 +01:00
import { UserModel } from '../models/user.mjs'
2022-10-31 17:54:49 +01:00
export function UsersController() {}
/*
* Signup
*
* This is the endpoint that handles account signups
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.signup = async (req, res, tools) => {
2022-11-03 19:56:06 +01:00
const User = new UserModel(tools)
2022-11-14 18:26:20 +01:00
await User.guardedCreate(req)
2022-10-31 17:54:49 +01:00
2022-11-03 19:56:06 +01:00
return User.sendResponse(res)
}
/*
* Confirm account (after signup)
*
* This is the endpoint that fully unlocks the account if the user gives their consent
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.confirm = async (req, res, tools) => {
2022-11-05 22:02:51 +01:00
const User = new UserModel(tools)
await User.confirm(req)
2022-11-05 22:02:51 +01:00
return User.sendResponse(res)
}
/*
* Sign in (with username and password)
*
* This is the endpoint that provides traditional username/password sign in
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.signin = async function (req, res, tools) {
const User = new UserModel(tools)
await User.passwordSignIn(req)
return User.sendResponse(res)
}
/*
* Send a magic link to sign in
*
* This is the endpoint that provides sign in via magic link
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.signinlink = async function (req, res, tools) {
const User = new UserModel(tools)
await User.sendSigninlink(req)
return User.sendResponse(res)
}
/*
* Sign in (with a sign-in link)
*
* This is the endpoint that signs in a user via a sign-in link (aka magic link)
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.signinvialink = async function (req, res, tools) {
const User = new UserModel(tools)
await User.linkSignIn(req)
return User.sendResponse(res)
}
/*
* Returns the account of the authenticated user (with JWT)
*
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.whoami = async (req, res, tools) => {
2022-11-08 21:04:32 +01:00
const User = new UserModel(tools)
2022-11-14 18:30:54 +01:00
await User.guardedRead({ id: req.user.uid }, req)
2022-11-08 21:04:32 +01:00
return User.sendResponse(res)
}
/*
* Updates the account of the authenticated user
*
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.update = async (req, res, tools) => {
2022-11-08 21:04:32 +01:00
const User = new UserModel(tools)
2022-11-14 18:30:54 +01:00
await User.guardedRead({ id: req.user.uid }, req)
await User.guardedUpdate(req)
2022-11-03 19:56:06 +01:00
return User.sendResponse(res)
2018-11-08 16:58:14 +01:00
}
2022-11-17 20:41:21 +01:00
/*
* Updates the MFA setting of the authenticated user
*
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.updateMfa = async (req, res, tools) => {
const User = new UserModel(tools)
await User.guardedRead({ id: req.user.uid }, req)
await User.guardedMfaUpdate(req)
return User.sendResponse(res)
}
2022-12-24 14:42:16 +01:00
/*
* Checks whether a submitted username is available
*
* See: https://freesewing.dev/reference/backend/api
*/
UsersController.prototype.isUsernameAvailable = async (req, res, tools) => {
const User = new UserModel(tools)
const available = await User.isLusernameAvailable(req.body.username.toLowerCase())
2022-12-24 14:42:16 +01:00
if (!available)
2022-12-24 14:42:16 +01:00
User.setResponse(200, false, {
result: 'success',
username: req.body?.username,
available: false,
})
else User.setResponse(404)
return User.sendResponse(res)
}