Commit 5a873b10 authored by vietanh-0511's avatar vietanh-0511

feature login

parent 6a0b13e8
<template> <template>
<div> <v-app>
<Nuxt /> <Nuxt />
</div> </v-app>
</template> </template>
<script> <script>
export default {}; export default {}
</script> </script>
<style>
</style>
\ No newline at end of file
...@@ -46,13 +46,16 @@ export default { ...@@ -46,13 +46,16 @@ export default {
'@nuxtjs/axios', '@nuxtjs/axios',
'@nuxtjs/auth-next', '@nuxtjs/auth-next',
'@nuxtjs/toast', '@nuxtjs/toast',
'@pinia/nuxt', '@pinia/nuxt'
], ],
// Axios module configuration: https://go.nuxtjs.dev/config-axios // Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: { axios: {
// Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308 // Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308
baseURL: '/', baseURL: 'http://127.0.0.1:8000/api/',
debug: false,
proxyHeaders: true,
credentials: false
}, },
// Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
...@@ -88,6 +91,6 @@ export default { ...@@ -88,6 +91,6 @@ export default {
} }
} }
] ]
}, }
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
"vue": "^2.7.10", "vue": "^2.7.10",
"vue-server-renderer": "^2.7.10", "vue-server-renderer": "^2.7.10",
"vue-template-compiler": "^2.7.10", "vue-template-compiler": "^2.7.10",
"vuelidate": "^0.7.7",
"vuetify": "^2.6.10" "vuetify": "^2.6.10"
}, },
"devDependencies": { "devDependencies": {
......
<template> <template>
<div style="background-color: #8fc2ff"> <div class="body">
<Notification :message="error" v-if="error" /> <form class="login-form">
<div <NuxtLogo />
class="container" <v-text-field
style=" v-model="email"
display: flex; :error-messages="emailErrors"
justify-content: center; label="E-mail"
align-items: center; required
height: 100vh; @input="$v.email.$touch()"
background-color: #8fc2ff; @blur="$v.email.$touch()"
" />
> <v-text-field
<div id="form-login"> v-model="password"
<form @submit.prevent="login"> :error-messages="passwordErrors"
<NuxtLogo /> :append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'"
<input :type="showPassword ? 'text' : 'password'"
id="email" label="Password"
v-model="email" hint="At least 6 characters"
type="email" required
class="form-control mb-3" @input="$v.password.$touch()"
placeholder="Email" @blur="$v.password.$touch()"
required @click:append="showPassword = !showPassword"
/> />
<input <v-checkbox
id="password" v-model="checkbox"
v-model="password" :error-messages="checkboxErrors"
type="password" label="Remember me"
class="form-control mb-3" required
placeholder="password" @change="$v.checkbox.$touch()"
oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" @blur="$v.checkbox.$touch()"
maxlength="16" />
min="6" <v-btn class="mr-4" @click="login">
required Login
/> </v-btn>
<b-form-checkbox <v-btn @click="clear">
id="checkbox-1" clear
v-model="status" </v-btn>
name="checkbox-1" </form>
value="accepted"
class="mb-2"
unchecked-value="not_accepted"
>
Remember me
</b-form-checkbox>
<div class="col text-center">
<button
class="w-30 btn btn-lg btn-primary text-center"
type="submit"
>
Sign in
</button>
</div>
</form>
</div>
</div>
</div> </div>
</template> </template>
<script> <script>
import notification from "@/components/notification"
export default { export default {
layout: "none", // eslint-disable-next-line no-undef
components: { notification }, components: { NuxtLogo }
components: { NuxtLogo }, }
};
</script> </script>
<script> <script>
import { reactive } from "vue"; import { validationMixin } from 'vuelidate'
import axios from "axios"; import { required, minLength, email } from 'vuelidate/lib/validators'
export default { export default {
layout: "none", mixins: [validationMixin],
data: () => { layout: 'empty',
return { validations: {
email: "", email: { required, email },
password: "", password: { required, minLength: minLength(6) },
status: null, checkbox: {
error: null, checked (val) {
}; return val
}
}
},
data: () => ({
email: '',
password: '',
showPassword: false,
checkbox: false
}),
computed: {
emailErrors () {
const errors = []
if (!this.$v.email.$dirty) {
return errors
}
!this.$v.email.email && errors.push('Must be valid e-mail')
!this.$v.email.required && errors.push('E-mail is required')
return errors
},
passwordErrors () {
const errors = []
if (!this.$v.password.$dirty) {
return errors
}
!this.$v.password.minLength && errors.push('Password must be at least 6 charactors')
!this.$v.password.required && errors.push('Password is required')
return errors
}
}, },
methods: { methods: {
async login() { submit () {
this.$v.$touch()
},
clear () {
this.$v.$reset()
this.email = ''
this.password = ''
this.checkbox = false
},
async login () {
try { try {
const resp = await fetch("http://127.0.0.1:8000/api/login", { const resp = await fetch('http://127.0.0.1:8000/api/login', {
method: "POST", method: 'POST',
headers: { headers: {
"Content-Type": "application/json", 'Content-Type': 'application/json',
Accept: "application/json", Accept: 'application/json'
}, },
body: JSON.stringify({ body: JSON.stringify({
email: this.email, email: this.email,
password: this.password, password: this.password,
status: this.status, status: this.status
}), })
}).then((response) => { }).then((response) => {
return response.json(); return response.json()
}); })
console.log(resp.status); console.log(resp.status)
localStorage.setItem("token", resp.data.bearer_token); localStorage.setItem('token', resp.data.bearer_token)
this.$auth.$storage.setUniversal("token", resp.data.bearer_token); this.$auth.$storage.setUniversal('token', resp.data.bearer_token)
this.$auth.$storage.setUniversal("userName", resp.data.name); this.$auth.$storage.setUniversal('userName', resp.data.name)
this.$auth.$storage.setUniversal("loggedIn", "true"); this.$auth.$storage.setUniversal('loggedIn', 'true')
if (resp.status == "success") { if (resp.status == 'success') {
this.$toast.success("Successfully authenticated", { this.$toast.success('Successfully authenticated', {
duration: 2000, duration: 2000
}); })
this.$router.push("home"); this.$router.push('home')
} }
} catch (e) { } catch (e) {
// this.error = "Username or Password not valid"; this.$toast.error('Username or Password not valid', {
this.$toast.error("Username or Password not valid", { duration: 2000
duration: 2000, })
}); this.$router.push('/login')
this.$router.push("/login");
}
},
checkForm: function (e) {
if (this.name && this.age) {
return true;
}
this.errors = [];
if (!this.name) {
this.errors.push("Name required.");
} }
if (!this.age) { }
this.errors.push("Age required."); }
} }
e.preventDefault();
},
},
};
</script> </script>
<style scoped> <style>
#form-login { .body {
height: 50%;
width: 25%;
padding: 12px;
padding-top: 60px;
display: flex; display: flex;
justify-content: center; justify-content: center;
background-color: white; text-align: center;
border-radius: 3%; align-items: center;
font-weight: 600; background-color: #2c2c2c;
height: 100vh;
}
.login-form{
width: 27%;
border: #8fc2ff 1px solid;
background-color: #121212;
border-radius: 1rem;
padding: 2rem 1rem;
border-radius: 1rem;
} }
</style> </style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment