-
Notifications
You must be signed in to change notification settings - Fork 5
/
SignIn.vue
327 lines (306 loc) · 11.3 KB
/
SignIn.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<template>
<div v-if="spinner" class="loading-blur">
<AppSpinner />
</div>
<div id="signin-container">
<section id="signin">
<header>
<div class="signin-logo">
<PvImage v-if="isLevante" src="/LEVANTE/Levante_Logo.png" alt="LEVANTE Logo" width="200" />
<ROARLogoShort v-else />
</div>
</header>
<h1 v-if="!isLevante">{{ $t('pageSignIn.welcome') }}</h1>
<section class="signin-options">
<section class="signin-option-container signin-option-userpass">
<h4 class="signin-option-title">{{ $t('pageSignIn.login') }}</h4>
<div id="languageSelect" class="m-4 flex justify-content-center">
<LanguageSelector />
</div>
<SignIn :invalid="incorrect" @submit="authWithEmail" @update:email="email = $event" />
</section>
<section class="flex flex-row align-content-center">
<h4 class="flex m-0 align-content-center justify-content-center mr-3 flex-wrap-reverse">
{{ $t('pageSignIn.loginWith') }}
</h4>
<div class="flex">
<PvButton
label="Sign in with Google"
class="flex surface-0 p-1 mr-1 border-black-alpha-10 w-full text-center justify-content-center hover:border-primary hover:surface-ground"
style="border-radius: 3rem; height: 3rem"
@click="authWithGoogle"
>
<img src="../assets/provider-google-logo.svg" alt="The Google Logo" class="flex mr-2 w-2" />
<span>Google</span>
</PvButton>
<PvButton
v-if="!isLevante"
class="flex surface-0 p-1 mr-1 border-black-alpha-10 w-full justify-content-center hover:border-primary hover:surface-ground"
style="border-radius: 3rem; height: 3rem"
@click="authWithClever"
>
<img src="../assets/provider-clever-logo.svg" alt="The Clever Logo" class="flex mr-2 w-2" />
<span>Clever</span>
</PvButton>
<PvButton
v-if="!isLevante"
class="flex surface-0 p-1 mr-1 border-black-alpha-10 w-full justify-content-center hover:border-primary hover:surface-ground"
style="border-radius: 3rem; height: 3rem"
@click="authWithClassLink"
>
<img src="../assets/provider-classlink-logo.png" alt="The ClassLink Logo" class="flex mr-2 w-2" />
<span>ClassLink</span>
</PvButton>
</div>
</section>
<!-- <section class="signin-option-container signin-option-providers">
<div class="flex flex-row justify-content-center w-full">
<p class="signin-option-title text-sm">Don't have an account yet?</p>
<PvButton label="Register" class="signin-button w-3" @click="router.push({ name: 'Register' })" />
</div>
</section> -->
</section>
<footer style="display: none">
<!-- TODO: figure out a link for this -->
<a href="#trouble">{{ $t('pageSignIn.havingTrouble') }}</a>
</footer>
</section>
</div>
<RoarModal
:is-enabled="warningModalOpen"
title="Email is already associated with an account"
subtitle=""
icon="pi-exclamation-triangle"
small
@modal-closed="handleWarningModalClose"
>
<template #default>
The email <span class="font-bold">{{ email }}</span> is already in use using
{{ displaySignInMethods.slice(0, -1).join(', ') + ' or ' + displaySignInMethods.slice(-1) }}. If this is you,
click to sign in below.
<div class="flex align-items-center flex-column gap-2 my-2">
<div v-if="signInMethods.includes('google')" class="flex">
<PvButton
label="Sign in with Google"
class="flex surface-0 p-1 mr-1 border-black-alpha-10 text-center justify-content-center hover:border-primary hover:surface-ground"
style="border-radius: 3rem; height: 3rem"
@click="authWithGoogle"
>
<img src="../assets/provider-google-logo.svg" alt="The Google Logo" class="flex mr-2 w-2" />
<span>Google</span>
</PvButton>
</div>
<div v-if="signInMethods.includes(AUTH_SSO_PROVIDERS.CLEVER)">
<PvButton
v-if="!isLevante"
class="flex surface-0 p-1 mr-1 border-black-alpha-10 justify-content-center hover:border-primary hover:surface-ground"
style="border-radius: 3rem; height: 3rem"
@click="authWithClever"
>
<img src="../assets/provider-clever-logo.svg" alt="The Clever Logo" class="flex mr-2 w-2" />
<span>Clever</span>
</PvButton>
</div>
<div v-if="signInMethods.includes(AUTH_SSO_PROVIDERS.CLASSLINK)">
<PvButton
v-if="!isLevante"
class="flex surface-0 p-1 mr-1 border-black-alpha-10 justify-content-center hover:border-primary hover:surface-ground"
style="border-radius: 3rem; height: 3rem"
@click="authWithClassLink"
>
<img src="../assets/provider-classlink-logo.png" alt="The ClassLink Logo" class="flex mr-2 w-2" />
<span>ClassLink</span>
</PvButton>
</div>
<div v-if="signInMethods.includes('password')" class="flex flex-row gap-2">
<PvPassword v-model="modalPassword" placeholder="Password" :feedback="false"></PvPassword>
<PvButton
class="flex p-3 border-none border-round hover:bg-black-alpha-20"
:label="$t('authSignIn.buttonLabel') + ' →'"
@click="authWithEmail({ email, password: modalPassword, useLink: false, usePassword: true })"
/>
</div>
</div>
You will then be directed to your profile page where you can link different authentication providers.
</template>
<template #footer>
<PvButton
tabindex="0"
class="border-none border-round bg-white text-primary p-2 hover:surface-200"
text
label="Back to Sign In"
outlined
@click="handleWarningModalClose"
></PvButton>
</template>
</RoarModal>
</template>
<script setup>
import { onMounted, ref, toRaw, onBeforeUnmount, computed } from 'vue';
import { storeToRefs } from 'pinia';
import { useRouter } from 'vue-router';
import SignIn from '@/components/auth/SignIn.vue';
import ROARLogoShort from '@/assets/RoarLogo-Short.vue';
import { useAuthStore } from '@/store/auth';
import { isMobileBrowser } from '@/helpers';
import { fetchDocById } from '../helpers/query/utils';
import RoarModal from '../components/modals/RoarModal.vue';
import { AUTH_SSO_PROVIDERS } from '@/constants/auth';
import { APP_ROUTES } from '@/constants/routes';
const incorrect = ref(false);
const isLevante = import.meta.env.MODE === 'LEVANTE';
const authStore = useAuthStore();
const router = useRouter();
const { spinner, ssoProvider, routeToProfile, roarfirekit } = storeToRefs(authStore);
const warningModalOpen = ref(false);
authStore.$subscribe(() => {
if (authStore.uid) {
if (authStore.userData && isLevante) {
if (
toRaw(authStore.userData?.userType?.toLowerCase()) === 'parent' ||
toRaw(authStore.userData?.userType?.toLowerCase()) === 'teacher'
) {
router.push({ name: 'Survey' });
return;
}
}
if (ssoProvider.value) {
router.push({ path: APP_ROUTES.SSO });
} else if (routeToProfile.value) {
router.push({ path: APP_ROUTES.ACCOUNT_PROFILE });
} else {
router.push({ path: APP_ROUTES.HOME });
}
}
});
const authWithGoogle = () => {
if (isMobileBrowser()) {
authStore.signInWithGoogleRedirect();
} else {
authStore
.signInWithGooglePopup()
.then(async () => {
if (authStore.uid) {
const userClaims = await fetchDocById('userClaims', authStore.uid);
authStore.userClaims = userClaims;
}
if (authStore.roarUid) {
const userData = await fetchDocById('users', authStore.roarUid);
authStore.userData = userData;
}
})
.catch((e) => {
const errorCode = e.code;
if (errorCode === 'auth/email-already-in-use') {
// User tried to register with an email that is already linked to a firebase account.
openWarningModal();
spinner.value = false;
} else {
console.log('caught error', e);
spinner.value = false;
}
});
spinner.value = true;
}
};
const modalPassword = ref('');
const authWithClever = () => {
console.log('---> authWithClever');
if (process.env.NODE_ENV === 'development' && !window.Cypress) {
authStore.signInWithCleverPopup();
} else {
authStore.signInWithCleverRedirect();
}
spinner.value = true;
};
const authWithClassLink = () => {
console.log('---> authWithClassLink');
if (isMobileBrowser()) {
authStore.signInWithClassLinkRedirect();
spinner.value = true;
} else {
authStore.signInWithClassLinkRedirect();
spinner.value = true;
}
};
const authWithEmail = (state) => {
// If username is supplied instead of email
// turn it into our internal auth email
incorrect.value = false;
let creds = toRaw(state);
if (creds.useLink && !creds.usePassword) {
authStore.initiateLoginWithEmailLink({ email: creds.email }).then(() => {
router.push({ name: 'AuthEmailSent' });
});
} else {
if (!creds.email.includes('@')) {
creds.email = `${creds.email}@roar-auth.com`;
}
authStore
.logInWithEmailAndPassword(creds)
.then(async () => {
if (authStore.uid) {
const userClaims = await fetchDocById('userClaims', authStore.uid);
authStore.userClaims = userClaims;
}
if (authStore.roarUid) {
const userData = await fetchDocById('users', authStore.roarUid);
authStore.userData = userData;
}
spinner.value = true;
})
.catch((e) => {
incorrect.value = true;
if (['auth/user-not-found', 'auth/wrong-password'].includes(e.code)) {
return;
} else {
throw e;
}
});
}
};
const handleWarningModalClose = () => {
authStore.routeToProfile = true;
warningModalOpen.value = false;
};
const email = ref('');
const signInMethods = ref([]);
const openWarningModal = async () => {
signInMethods.value = await roarfirekit.value.fetchEmailAuthMethods(email.value);
warningModalOpen.value = true;
};
const displaySignInMethods = computed(() => {
return signInMethods.value.map((method) => {
if (method === 'password') return 'Password';
if (method === AUTH_SSO_PROVIDERS.GOOGLE) return 'Google';
if (method === AUTH_SSO_PROVIDERS.CLEVER) return 'Clever';
if (method === AUTH_SSO_PROVIDERS.CLASSLINK) return 'ClassLink';
});
});
onMounted(() => {
document.body.classList.add('page-signin');
if (authStore.cleverOAuthRequested) {
authStore.cleverOAuthRequested = false;
authWithClever();
}
if (authStore.classLinkOAuthRequested) {
authStore.classLinkOAuthRequested = false;
authWithClassLink();
}
});
onBeforeUnmount(() => {
document.body.classList.remove('page-signin');
});
</script>
<style scoped>
.loading-blur {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;
background-color: rgba(255, 255, 255, 0.7);
padding-top: 21vh;
}
</style>