-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
121 lines (102 loc) · 3.16 KB
/
auth.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
"use strict";
import { APIGatewayEvent } from "aws-lambda";
import { serverless_DTO } from "./DTO";
import { UserModel } from "./src/model/users";
import { CodeModel } from "./src/model/code";
import { getAccessTokenByCode, getUserByToken } from "./src/util/github";
import { generateToken, verifyToken } from "./src/util/token";
import { sendAuthMessage } from "./src/util/email";
import {
createUser,
createToken,
updateUserInformation,
findUserByNickname,
testIsGSMEmail,
} from "./util/user";
import { connectMongoDB } from "./src/util/db";
const createRes: Function = (
status: number,
body?: Object,
headers?: Object,
): serverless_DTO.Response => {
return {
statusCode: status,
body: JSON.stringify(body),
headers: headers,
};
};
exports.authUserByOAuth = async (
event: APIGatewayEvent,
_: any,
__: Function,
) => {
const data = event.queryStringParameters;
const access_token = (await getAccessTokenByCode(data.code)).access_token;
const { name, nickname } = await getUserByToken(access_token);
const code = generateToken({ nickname: nickname }, "180m");
let page = "complete.html";
const user = await findUserByNickname(nickname);
if (!user?.certified) {
if (!user) {
await createUser({
accessToken: access_token,
name: name ?? " ",
nickname: nickname,
});
}
page = "email_auth.html";
}
return createRes(
302,
{},
{ Location: `${process.env.AUTH_BASEURL}${page}?code=${code}` },
);
};
exports.authEmail = async (event: APIGatewayEvent, _: any, __: Function) => {
const searchPrams = new URLSearchParams(event.body);
const code = searchPrams.get("code");
const email = searchPrams.get("email");
if (testIsGSMEmail(email)) {
return createRes(400, { detail: "GSM 학생 계정이어야합니다." });
}
const nickname = verifyToken(code).nickname;
const token = await createToken({ email: email, nickname: nickname });
await sendAuthMessage({
receiver: email,
nickname: nickname,
token: token.id,
});
return createRes(204);
};
exports.authUserByEmail = connectMongoDB(
async (event: APIGatewayEvent, _: any) => {
const dataId = event.pathParameters["token"];
const data = await CodeModel.findById(dataId);
const email: string = data.email;
const nickname: string = data.nickname;
const generation: number = testIsGSMEmail(email)
? Number(email.replace(/[^0-9]/g, "").slice(0, 2)) - 16
: 0;
if (generation === 0) {
return createRes(404, { message: "GSM 학생이 아닙니다." });
}
const user = await UserModel.findUserFromNickname(nickname);
try {
await user.updateGeneration(generation);
console.log("Success update Generation");
await user.setCertifiedTrue();
console.log("Success Set Certified True");
await updateUserInformation(user);
console.log("Update User Information");
} catch (e: any) {
console.error(e);
}
await CodeModel.findByIdAndDelete(dataId);
console.log("Success find By Id and delete data Id");
return createRes(
302,
{},
{ Location: `${process.env.AUTH_BASEURL}complete.html` },
);
},
);