/
api.ts
146 lines (138 loc) · 3.29 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import express from 'express';
import passport from 'passport';
import {Strategy as LocalStrategy} from 'passport-local';
import Salon from '../models/salon.js';
import {retrieveStripeAccount} from './middleware.js';
export const router = express.Router();
/**
* GET /api/session
*
* Return the logged-in salon and their Stripe account if available.
*/
router.get('/session', async (req, res) => {
try {
if (req.user) {
let stripeAccount = null;
// Get the stripe account object if available
if (req.user.stripeAccountId) {
stripeAccount = await retrieveStripeAccount(req.user.stripeAccountId);
}
res.status(200);
return res.json({
user: req.user,
stripeAccount,
});
} else {
res.status(200);
return res.json({
user: null,
stripeAccount: null,
});
}
} catch (error: any) {
console.error(error);
res.status(500);
return res.send({error: error.message});
}
});
/**
* POST /api/signup
*
* Create a user during the salon onboarding process.
*/
router.post('/signup', async (req, res, next) => {
const body = Object.assign({}, req.body, {
// Use `type` instead of `salon-type` for saving to the DB.
type: req.body['salon-type'],
'salon-type': undefined,
});
// Check if we have a logged-in salon
let salon = req.user;
if (!salon) {
try {
// Try to create and save a new salon
salon = new Salon(body);
salon = await salon!.save();
// Sign in and redirect to continue the signup process
req.logIn(salon, (err) => {
if (err) next(err);
return res.status(200).end();
});
} catch (err: any) {
console.error(err);
// Show an error message to the user
const errors = Object.keys(err.errors).map(
(field) => err.errors[field].message
);
return res.status(500).json({
error: errors[0],
});
}
}
});
/**
* POST /api/login
*
* Simple salon login.
*/
router.post(
'/login',
passport.authenticate('salon-login'),
function (req, res) {
if (req.user) {
res.status(200).end();
} else {
res.status(500).end();
}
}
);
/**
* POST /api/logout
*
* Delete the salon from the session.
*/
router.post('/logout', (req, res, next) => {
req.logout((err) => {
if (err) {
return res.status(500).send({error: err.message});
}
return res.status(200).end();
});
});
// Serialize the salon's sessions for Passport
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
let user = await Salon.findById(id);
done(null, user);
} catch (err) {
done(err);
}
});
// Define the login strategy for salons based on email and password
passport.use(
'salon-login',
new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
},
async (email, password, done) => {
let user;
try {
user = await Salon.findOne({email});
if (!user) {
return done(null, false, {message: 'Unknown user'});
}
} catch (err) {
return done(err);
}
if (!user.validatePassword(password)) {
return done(null, false, {message: 'Invalid password'});
}
return done(null, user);
}
)
);