-
Notifications
You must be signed in to change notification settings - Fork 10
/
AuthView.vue
197 lines (174 loc) · 7.29 KB
/
AuthView.vue
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<script lang="ts">
import ThirdParty from "supertokens-web-js/recipe/thirdparty";
import EmailPassword from "supertokens-web-js/recipe/emailpassword";
import Session from "supertokens-web-js/recipe/session";
import { defineComponent } from "vue";
import { apiDomain } from "../main";
const websitePort = import.meta.env.VUE_APP_WEB_PORT || 3000;
const websiteDomain = import.meta.env.VUE_APP_WEB_URL || `http://localhost:${websitePort}`;
export default defineComponent({
data() {
return {
// we allow the user to switch between sign in and sign up view
isSignIn: true,
// this will store the email and password entered by the user.
email: "",
password: "",
// any generic error states
error: false,
errorMessage: "Something went wrong",
// any error states specific to the input fields.
emailError: "",
passwordError: "",
};
},
mounted() {
// if there is an "error" query param on this page, it means that
// social login has failed for some reason. See the AuthCallbackView.vue file
// for more context on this
const params = new URLSearchParams(window.location.search);
if (params.has("error")) {
this.errorMessage = "Something went wrong";
this.error = true;
}
// this redirects the user to the HomeView.vue component if a session
// already exists.
this.checkForSession();
},
methods: {
goToSignUp() {
this.isSignIn = false;
},
goToSignIn() {
this.isSignIn = true;
},
signIn: async function (_: Event) {
const response = await EmailPassword.signIn({
formFields: [
{
id: "email",
value: this.email,
},
{
id: "password",
value: this.password,
},
],
});
if (response.status === "WRONG_CREDENTIALS_ERROR") {
// the input email / password combination did not match,
// so we show an appropriate error message to the user
this.errorMessage = "Invalid credentials";
this.error = true;
return;
}
if (response.status === "FIELD_ERROR") {
response.formFields.forEach((item) => {
if (item.id === "email") {
// this means that something was wrong with the entered email.
// probably that it's not a valid email (from a syntax point of view)
this.emailError = item.error;
} else if (item.id === "password") {
this.passwordError = item.error;
}
});
return;
}
// login is successful, and we redirect the user to the home page.
// Note that session cookies are added automatically and nothing needs to be
// done here about them.
window.location.assign("/");
},
validateEmail(email: string) {
return email
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
},
signUp: async function (_: Event) {
const response = await EmailPassword.signUp({
formFields: [
{
id: "email",
value: this.email,
},
{
id: "password",
value: this.password,
},
],
});
if (response.status === "FIELD_ERROR") {
response.formFields.forEach((item) => {
if (item.id === "email") {
// this means that something was wrong with the entered email.
// probably that it's not a valid email (from a syntax point of view)
this.emailError = item.error;
} else if (item.id === "password") {
// this means that something was wrong with the entered password.
// probably it doesn't meet the password validation criteria on the backend.
this.passwordError = item.error;
}
});
return;
}
// signup is successful, and we redirect the user to the home page.
// Note that session cookies are added automatically and nothing needs to be
// done here about them.
window.location.assign("/");
},
onSubmitPressed: function (e: Event) {
e.preventDefault();
// we reset the error states in case the user has fixed the input errors
this.error = false;
this.emailError = "";
this.passwordError = "";
if (this.isSignIn) {
this.signIn(e);
} else {
this.signUp(e);
}
},
onGithubPressed: async function () {
const authUrl = await ThirdParty.getAuthorisationURLWithQueryParamsAndSetState({
thirdPartyId: "github",
// This is where github should redirect the user back after login or error.
// This URL goes on the github dashboard as well.
frontendRedirectURI: `${websiteDomain}/auth/callback/github`,
});
window.location.assign(authUrl);
},
onGooglePressed: async function () {
const authUrl = await ThirdParty.getAuthorisationURLWithQueryParamsAndSetState({
thirdPartyId: "google",
// This is where google should redirect the user back after login or error.
// This URL goes on the google dashboard as well.
frontendRedirectURI: `${websiteDomain}/auth/callback/google`,
});
window.location.assign(authUrl);
},
onApplePressed: async function () {
const authUrl = await ThirdParty.getAuthorisationURLWithQueryParamsAndSetState({
thirdPartyId: "apple",
// This is where apple should redirect the user back after login or error.
// This URL goes on the apple dashboard as well.
frontendRedirectURI: `${websiteDomain}/auth/callback/apple`,
redirectURIOnProviderDashboard: `${apiDomain}/auth/callback/apple`,
});
window.location.assign(authUrl);
},
checkForSession: async function () {
if (await Session.doesSessionExist()) {
// since a session already exists, we redirect the user to the HomeView.vue component
window.location.assign("/");
}
},
},
});
</script>
<template src="../html/authView.html"></template>
<style>
@import "@/assets/base.css";
@import "../css/authview.css";
</style>