2022-11-03 19:56:06 +01:00
|
|
|
import { UserModel } from '../models/user.mjs'
|
2022-10-31 17:54:49 +01:00
|
|
|
|
2022-11-16 15:07:24 +01:00
|
|
|
export function UsersController() {}
|
2022-01-01 15:18:27 +01:00
|
|
|
|
2022-11-01 18:00:25 +01:00
|
|
|
/*
|
|
|
|
* Signup
|
|
|
|
*
|
|
|
|
* This is the endpoint that handles account signups
|
|
|
|
* See: https://freesewing.dev/reference/backend/api
|
|
|
|
*/
|
2022-11-16 15:07:24 +01:00
|
|
|
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)
|
2022-11-01 18:00:25 +01:00
|
|
|
}
|
2022-10-29 22:21:24 +02:00
|
|
|
|
2022-11-01 18:00:25 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2022-11-16 15:07:24 +01:00
|
|
|
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-01 21:36:15 +01:00
|
|
|
|
2022-11-05 22:02:51 +01:00
|
|
|
return User.sendResponse(res)
|
2022-11-01 18:00:25 +01:00
|
|
|
}
|
2022-11-01 21:36:15 +01:00
|
|
|
|
2022-11-01 18:00:25 +01:00
|
|
|
/*
|
2023-03-19 16:59:26 +01:00
|
|
|
* Sign in (with username and password)
|
2022-11-02 12:33:32 +01:00
|
|
|
*
|
2023-03-19 16:59:26 +01:00
|
|
|
* This is the endpoint that provides traditional username/password sign in
|
2022-11-02 12:33:32 +01:00
|
|
|
* See: https://freesewing.dev/reference/backend/api
|
|
|
|
*/
|
2023-03-19 16:59:26 +01:00
|
|
|
UsersController.prototype.signin = async function (req, res, tools) {
|
2022-11-07 20:42:07 +01:00
|
|
|
const User = new UserModel(tools)
|
2023-03-19 16:59:26 +01:00
|
|
|
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)
|
2022-11-02 12:33:32 +01:00
|
|
|
|
2022-11-07 20:42:07 +01:00
|
|
|
return User.sendResponse(res)
|
2022-01-01 15:18:27 +01:00
|
|
|
}
|
2022-11-02 13:54:20 +01:00
|
|
|
|
2023-03-19 19:04:15 +01:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:41:30 +01:00
|
|
|
/*
|
|
|
|
* Returns the account of the authenticated user (with JWT)
|
|
|
|
*
|
|
|
|
* See: https://freesewing.dev/reference/backend/api
|
|
|
|
*/
|
2022-11-16 15:07:24 +01:00
|
|
|
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-02 13:54:20 +01:00
|
|
|
|
2022-11-08 21:04:32 +01:00
|
|
|
return User.sendResponse(res)
|
2022-11-02 13:54:20 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 22:41:30 +01:00
|
|
|
/*
|
|
|
|
* Updates the account of the authenticated user
|
|
|
|
*
|
|
|
|
* See: https://freesewing.dev/reference/backend/api
|
|
|
|
*/
|
2022-11-16 15:07:24 +01:00
|
|
|
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
|
|
|
|
2022-11-08 22:41:30 +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)
|
2023-01-24 21:17:49 +01:00
|
|
|
const available = await User.isLusernameAvailable(req.body.username.toLowerCase())
|
2022-12-24 14:42:16 +01:00
|
|
|
|
2023-01-24 21:17:49 +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)
|
|
|
|
}
|