Skip to content

Commit b2f1f04

Browse files
committed
sign in frontend hooked with backend
1 parent b2ff554 commit b2f1f04

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

frontend/src/views/auth/SignIn.vue

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<!-- eslint-disable max-len -->
33
<div class="auth-container">
4+
<div class="auth-sub-container-error mb-4" v-if="error.happened">
5+
<h1 class="auth-sub-container-error-message">{{error.message}}</h1>
6+
</div>
47
<div class="auth-sub-container">
58
<div class="auth-sub-container-content">
69
<div class="flex">
@@ -11,7 +14,7 @@
1114
<div class="auth-sub-container-content mt-4">
1215
<ValidationObserver v-slot="{ handleSubmit, invalid }">
1316
<form @submit.prevent="handleSubmit(onSubmit)">
14-
<ValidationProvider name="Username" rules="required|max:20" v-slot="{errors}">
17+
<ValidationProvider name="Username" rules="required|max:100" v-slot="{errors}">
1518
<input type="text" placeholder="Username" v-model="formData.username" class="matcha-input">
1619
<span class="matcha-input-error">{{ errors[0] }}</span>
1720
</ValidationProvider>
@@ -37,17 +40,52 @@
3740
</template>
3841

3942
<script>
43+
import jwtDecode from 'jwt-decode';
44+
import { setAccessToken, setRefreshToken } from '../../auth/tokens';
4045
4146
export default {
4247
data: () => ({
4348
formData: {
4449
username: '',
4550
password: '',
4651
},
52+
error: {
53+
happened: false,
54+
message: '',
55+
},
4756
}),
4857
methods: {
49-
onSubmit() {
50-
console.log('nice');
58+
async onSubmit() {
59+
try {
60+
this.clearError();
61+
const response = await this.signInUser(this.formData);
62+
setAccessToken(response.data.return.access_token);
63+
setRefreshToken(response.data.return.refresh_token);
64+
await this.$store.dispatch('login', this.getUserFromJwt(response.data.return.access_token));
65+
if (response.data.return.is_profile_completed) {
66+
this.$router.push('/browse');
67+
} else {
68+
this.$router.push('/onboarding');
69+
}
70+
} catch (error) {
71+
this.displayError(this.$errorMessenger(error));
72+
}
73+
},
74+
async signInUser(user) {
75+
const response = await this.$http.post('/auth/login', user);
76+
return response;
77+
},
78+
getUserFromJwt(token) {
79+
const { identity } = jwtDecode(token);
80+
return identity;
81+
},
82+
clearError() {
83+
this.error.message = '';
84+
this.error.happened = false;
85+
},
86+
displayError(message) {
87+
this.error.message = message;
88+
this.error.happened = true;
5189
},
5290
},
5391
};

0 commit comments

Comments
 (0)