|
1 | 1 | <template> |
2 | 2 | <!-- eslint-disable max-len --> |
3 | 3 | <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> |
4 | 7 | <div class="auth-sub-container"> |
5 | 8 | <div class="auth-sub-container-content"> |
6 | 9 | <div class="flex"> |
|
11 | 14 | <div class="auth-sub-container-content mt-4"> |
12 | 15 | <ValidationObserver v-slot="{ handleSubmit, invalid }"> |
13 | 16 | <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}"> |
15 | 18 | <input type="text" placeholder="Username" v-model="formData.username" class="matcha-input"> |
16 | 19 | <span class="matcha-input-error">{{ errors[0] }}</span> |
17 | 20 | </ValidationProvider> |
|
37 | 40 | </template> |
38 | 41 |
|
39 | 42 | <script> |
| 43 | +import jwtDecode from 'jwt-decode'; |
| 44 | +import { setAccessToken, setRefreshToken } from '../../auth/tokens'; |
40 | 45 |
|
41 | 46 | export default { |
42 | 47 | data: () => ({ |
43 | 48 | formData: { |
44 | 49 | username: '', |
45 | 50 | password: '', |
46 | 51 | }, |
| 52 | + error: { |
| 53 | + happened: false, |
| 54 | + message: '', |
| 55 | + }, |
47 | 56 | }), |
48 | 57 | 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; |
51 | 89 | }, |
52 | 90 | }, |
53 | 91 | }; |
|
0 commit comments