-
Notifications
You must be signed in to change notification settings - Fork 2
/
Login.vue
158 lines (155 loc) · 5.52 KB
/
Login.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
<template>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="card">
<div class="card-title text-center">
<h2 class="p-3">Login</h2>
<div
v-if="error"
class="alert alert-danger"
role="alert"
>
Incorrect username or password
</div>
</div>
<div class="card-body">
<form @submit.prevent="submit">
<div class="mb-3">
<label for="username" class="form-label"
>Username:</label
>
<input
type="text"
name="username"
v-model="username"
class="form-control"
/>
</div>
<div class="mb-3">
<label for="password" class="form-label"
>Password:</label
>
<input
type="password"
name="password"
v-model="password"
class="form-control"
/>
</div>
<button type="submit" class="btn btn-primary">
Submit
</button>
<p class="float-end">
Don't have account
<router-link to="/register"
>Sign Up</router-link
>
</p>
</form>
<div class="card-title text-center">
<br />
<div class="seperator">
<hr />
<b>OR</b>
</div>
<GoogleLogin
class="btn btn-google"
:params="params"
:renderParams="renderParams"
:onSuccess="onSuccess"
></GoogleLogin>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import { mapActions } from "vuex"
import GoogleLogin from "vue-google-login"
export default {
data() {
return {
username: "",
password: "",
error: false,
params: {
client_id: process.env.VUE_APP_CLIENT_ID
},
renderParams: {
width: 260,
height: 50,
longtitle: true
}
}
},
components: {
GoogleLogin
},
methods: {
...mapActions(["loginUser", "registerUser"]),
async submit() {
const form = new FormData()
form.append("username", this.username)
form.append("password", this.password)
await this.$store
.dispatch("loginUser", form)
.then(() => {
this.$router.push("/")
})
.catch(() => {
this.error = true
})
},
onSuccess(googleUser) {
// data for register Google user as BaseUser with fake password
// to satisfy Pydantic validation on backend
let password = "0425346c32c9c3dd"
let userInfo = {
username: googleUser.getBasicProfile().getName(),
email: googleUser.getBasicProfile().getEmail(),
password: password
}
// form for login Google user as BaseUser with fake password because
// we don't need a password when using a Google login. We can reveal
// this password because is hashed by Piccolo backend anyway,
// and we actually aren’t use this password. With this way both
// username/password login and Google login can work together
let loginForm = new FormData()
loginForm.append("username", googleUser.getBasicProfile().getName())
loginForm.append("password", password)
this.$store
.dispatch("registerUser", userInfo)
.then(() => {
this.$router.push("/")
})
.catch(() => {
this.$store.dispatch("loginUser", loginForm)
this.$router.push("/")
})
}
}
}
</script>
<style lang="less" scoped>
.seperator b {
width: 40px;
height: 40px;
font-size: 16px;
text-align: center;
line-height: 40px;
background: #fff;
display: inline-block;
border: 1px solid #e0e0e0;
border-radius: 50%;
position: relative;
top: -36px;
z-index: 1;
margin-bottom: -1rem;
}
.float-end {
padding-top: 0.5rem;
}
</style>