-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauth.ts
89 lines (75 loc) · 2.39 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
import { JWT, auth } from "@colyseus/auth";
import { User } from "./database";
import { resend } from "./email";
/**
* Email / Password Authentication
*/
auth.settings.onFindUserByEmail = async (email) => {
console.log("@colyseus/auth: onFindByEmail =>", { email });
return await User.query()
.selectAll()
.where("email", "=", email)
.executeTakeFirst();
};
auth.settings.onRegisterWithEmailAndPassword = async (email, password, options) => {
console.log("@colyseus/auth: onRegister =>", { email, password, ...options });
// Validate custom "options"
const additionalData: any = {};
if (options.birthdate) { additionalData.birthdate = options.birthdate; }
if (options.custom_data) { additionalData.custom_data = JSON.stringify(options.custom_data); }
if (options.locale) { additionalData.locale = options.locale; }
const name = options.name || email.split("@")[0];
return await User.insert({
name,
email,
password,
...additionalData,
});
}
auth.settings.onSendEmailConfirmation = async (email, html, link) => {
await resend.emails.send({
from: 'web-template@colyseus.io',
to: email,
subject: '[Colyseus Web Template]: Reset password',
html,
});
}
auth.settings.onForgotPassword = async (email: string, html: string/* , resetLink: string */) => {
await resend.emails.send({
from: 'web-template@colyseus.io',
to: email,
subject: '[Colyseus Web Template]: Reset password',
html,
});
}
auth.settings.onResetPassword = async (email: string, password: string) => {
await User.update({ password }).where("email", "=", email).execute();
}
/**
* OAuth providers
*/
auth.oauth.addProvider('discord', {
key: process.env.DISCORD_CLIENT_ID,
secret: process.env.DISCORD_CLIENT_SECRET,
scope: ['identify', 'email'],
});
// auth.oauth.defaults.origin = "http://localhost:2567";
auth.oauth.addProvider('twitch', {
key: "3a3311zpk0vimb7mjxtw7n1axbidtx",
secret: "s78w1l2tyeyvxz25ypovn26q90ky53",
scope: ['user:read:email'],
});
auth.oauth.onCallback(async (data, provider) => {
const profile = data.profile;
console.log("DATA:", data);
console.log("PROFILE:", profile);
return await createUser(profile);
});
export function createUser(profile: any) {
return User.upsert({
discord_id: profile.id,
name: profile.global_name || profile.username || profile.login,
locale: profile.locale || "",
email: profile.email,
})
}