auth.ctrl.js
1.81 KB
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
//회원가입, 로그인 및 로그아웃에 관한 api
const User = require('../../models/user');
const Joi = require('joi');
exports.register = async(ctx) => {
const { userId, password, passwordCheck } = ctx.request.body;
const schema = Joi.object().keys({
userId : Joi.string().email().max(50).required(),
password : Joi.string().required(),
passwordCheck : Joi.string().required(),
})
const result = schema.validate(ctx.request.body);
if(result.error || password !== passwordCheck) {
ctx.status = 400;
return;
}
const existUser = await User.findByUserId(userId);
if(existUser) {
ctx.status = 409;
return;
}
const user = new User({
userId
});
await user.setPassword(password);
await user.save();
ctx.status = 201;
};
exports.login = async(ctx) => {
const { userId, password } = ctx.request.body;
const schema = Joi.object().keys({
userId : Joi.string().email().max(50).required(),
password : Joi.string().required()
})
const result = schema.validate(ctx.request.body);
if(result.error) {
ctx.status = 400;
return;
}
const user = await User.findByUserId(userId);
if(!user) {
ctx.stauts = 401;
return;
}
const isPasswordTrue = await user.checkPassword(password);
if(!isPasswordTrue) {
ctx.status = 401;
return;
}
const token = await user.generateToken();
ctx.cookies.set('access_token', token, {
httpOnly : true,
maxAge : 1000 * 60 * 60 * 24 * 30
});
ctx.status = 200;
ctx.body = {
userId
};
};
exports.logout = async(ctx) => {
ctx.cookies.set('access_token', null, {
httpOnly : true,
maxAge : 0
});
ctx.status = 204;
};