Commit 2250c2ba authored by TTS Kieu Tuan Anh's avatar TTS Kieu Tuan Anh

fix bug auth

parent 817ac51e
<template> <template>
<div> <div>
<b-navbar toggleable="lg" type="dark" variant="info"> <b-navbar toggleable="lg" type="dark" variant="info">
<b-navbar-brand href="#">NavBar</b-navbar-brand> <b-navbar-brand href="#">NavBar</b-navbar-brand>
...@@ -14,8 +14,14 @@ ...@@ -14,8 +14,14 @@
<!-- Right aligned nav items --> <!-- Right aligned nav items -->
<b-navbar-nav class="ml-auto"> <b-navbar-nav class="ml-auto">
<b-nav-form> <b-nav-form>
<b-form-input size="sm" class="mr-sm-2" placeholder="Search"></b-form-input> <b-form-input
<b-button size="sm" class="my-2 my-sm-0" type="submit">Search</b-button> size="sm"
class="mr-sm-2"
placeholder="Search"
></b-form-input>
<b-button size="sm" class="my-2 my-sm-0" type="submit"
>Search</b-button
>
</b-nav-form> </b-nav-form>
<b-nav-item-dropdown text="Lang" right> <b-nav-item-dropdown text="Lang" right>
...@@ -36,27 +42,18 @@ ...@@ -36,27 +42,18 @@
</b-navbar-nav> </b-navbar-nav>
</b-collapse> </b-collapse>
</b-navbar> </b-navbar>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: 'nav' name: "nav",
} };
</script> </script>
<script> <script>
const token = localStorage.getItem('bgcolor'); const token_access = localStorage.getItem("token");
export default { export default {
name: "logout", name: "logout",
data() {
return {
users: [],
}
},
methods: { methods: {
logout() { logout() {
fetch("http://127.0.0.1:8000/api/logout", { fetch("http://127.0.0.1:8000/api/logout", {
...@@ -65,13 +62,14 @@ export default { ...@@ -65,13 +62,14 @@ export default {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
bearer_token: token, token: token_access,
}), }),
}); });
this.$router.push('/'); localStorage.removeItem("token");
this.$router.push("/");
}, },
}, },
};</script> };
</script>
<style> <style></style>
</style>
\ No newline at end of file
<template>
<div class="notification is-danger bg-danger">
{{ message }}
</div>
</template>
<style>
.notification {
padding: 11px;
margin: 10px 0px;
}
</style>
<script>
export default {
name: 'Notification',
props: ['message']
}
</script>
\ No newline at end of file
const token_access = localStorage.getItem('token');
export default function () {
if (!token_access) {
// this.$router.push('/')
console.log('[LOG]: running ...')
}
}
\ No newline at end of file
...@@ -44,11 +44,20 @@ export default { ...@@ -44,11 +44,20 @@ export default {
'@nuxtjs/axios', '@nuxtjs/axios',
'@nuxtjs/auth-next', '@nuxtjs/auth-next',
], ],
axios: {
baseURL: process.env.API_URL || ' http://localhost:8000/api/',
debug: process.env.DEBUG || false,
proxyHeaders: false,
credentials: false,
},
auth: { auth: {
// Options
}, },
// Build Configuration: https://go.nuxtjs.dev/config-build // Build Configuration: https://go.nuxtjs.dev/config-build
build: { build: {
},
router: {
middleware: 'web'
} }
} }
This diff is collapsed.
{ {
"name": "KiaiSoft_Tuan_Anh", "name": "kiaisoft_tuan_anh",
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
}, },
"dependencies": { "dependencies": {
"@nuxtjs/auth-next": "5.0.0-1667386184.dfbbb54", "@nuxtjs/auth-next": "5.0.0-1667386184.dfbbb54",
"@nuxtjs/axios": "^5.13.6", "@nuxtjs/axios": "^5.0.0",
"axios": "^1.2.1", "axios": "^1.2.1",
"bootstrap": "^4.6.2", "bootstrap": "^4.6.2",
"bootstrap-vue": "^2.23.1", "bootstrap-vue": "^2.23.1",
...@@ -26,5 +26,14 @@ ...@@ -26,5 +26,14 @@
"devDependencies": { "devDependencies": {
"sass": "^1.57.1", "sass": "^1.57.1",
"sass-loader": "^10.4.1" "sass-loader": "^10.4.1"
} },
"description": "## Build Setup",
"main": "nuxt.config.js",
"repository": {
"type": "git",
"url": "https://gitlab.kiaisoft.com/kiaisoft.anh.tran2/kiaisoft_tuananh_nuxt.git"
},
"keywords": [],
"author": "",
"license": "ISC"
} }
...@@ -38,6 +38,12 @@ export default { ...@@ -38,6 +38,12 @@ export default {
// <script> // <script>
// console.log(data); // console.log(data);
</script> </script>
<script>
export default {
middleware: "web"
}
</script>
<style> <style>
</style> </style>
<template> <template>
<div> <div>
<Notification :message="error" v-if="error"/>
<div <div
class="container" class="container"
style=" style="
...@@ -39,51 +40,29 @@ ...@@ -39,51 +40,29 @@
<script> <script>
import Nav from "@/components/Nav"; import Nav from "@/components/Nav";
import Notification from '@/components/Notification';
export default { export default {
components: { Nav }, components: { Nav },
components: {Notification}
}; };
</script> </script>
<script> <script>
import { reactive } from "vue"; import { reactive } from "vue";
import axios from 'axios';
// export default {
// name: 'login',
// setup() {
// const data = reactive({
// email: '',
// password: '',
// checked: ''
// });
// const submit = async () => {
// await fetch('http://localhost:3000/api/', {
// method: 'POST',
// headers: {'Content-Type': 'application/json'},
// credentials: 'include',
// body: JSON.stringify(data)
// });
// await router.push('/');
// }
// return {
// data,
// submit
// }
// }
// }
export default { export default {
data: () => { data: () => {
return { return {
email: "", email: "",
password: "", password: "",
error: null
}; };
}, },
methods: { methods: {
async login() { async login() {
var resp = await fetch("http://127.0.0.1:8000/api/login", { try {
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",
...@@ -94,16 +73,37 @@ export default { ...@@ -94,16 +73,37 @@ export default {
}), }),
}).then((response) => { }).then((response) => {
return response.json(); return response.json();
if(resp.data.bearer_token){
this.$router.push('home');
}
}); });
console.log(resp.data.bearer_token) console.log(resp.status);
// localStorage.setItem('bgcolor', resp.data.bearer_token); localStorage.setItem('token', resp.data.bearer_token);
await this.$auth.setToken('local', "Bearer " + resp.data.bearer_token); if(resp.status == "success"){
await this.$auth.setRefreshToken('local', resp.data.refresh);
await this.$auth.setUserToken(resp.data.access); this.$router.push('home')
}
} catch (e) {
this.error = 'Username or Password not valid'
}
// async login() {
// try {
// const response = await this.$axios.post('http://127.0.0.1:8000/api/login', {
// email: this.email,
// password: this.password
// });
// await this.$auth.setToken('local', "Bearer " + response.data.access);
// await this.$auth.setRefreshToken('local', response.data.refresh);
// await this.$auth.setUserToken(response.data.access);
// } catch (e) {
// this.error = 'Username or Password not valid'
// }
}, },
......
<template> <template>
<div class="container" <div
class="container"
style=" style="
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
height: 100vh; height: 100vh;
"> "
>
<div id="form-group"> <div id="form-group">
<form @submit.prevent="signin"> <form @submit.prevent="signin">
<label>Name :</label> <label>Name :</label>
<input type="text" class="form-control" <input
placeholder="name" v-model="name" max="255" min="1" required> type="text"
class="form-control"
placeholder="name"
v-model="name"
max="255"
min="1"
required
/>
<label>Email :</label> <label>Email :</label>
<input type="email" class="form-control" <input
placeholder="Email" v-model="email" required> type="email"
class="form-control"
placeholder="Email"
v-model="email"
required
/>
<label>Password :</label> <label>Password :</label>
<input type="password" <input
type="password"
class="form-control" class="form-control"
placeholder="password" v-model="password" required> placeholder="password"
v-model="password"
required
/>
<label>Confirm Password :</label> <label>Confirm Password :</label>
<input type="password" <input
type="password"
class="form-control" class="form-control"
placeholder="Confirm_password" required> placeholder="Confirm_password"
required
/>
<div class="button"> <div class="button">
<button class="w-75 btn btn-lg btn-primary" type="submit">Sign up here</button> <button class="w-75 btn btn-lg btn-primary" type="submit">
Sign up here
</button>
</div> </div>
</form> </form>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import { reactive } from "vue"; import { reactive } from "vue";
...@@ -56,7 +78,7 @@ export default { ...@@ -56,7 +78,7 @@ export default {
password: this.password, password: this.password,
}), }),
}); });
this.$router.push('/'); this.$router.push("/");
}, },
}, },
}; };
...@@ -79,5 +101,5 @@ export default { ...@@ -79,5 +101,5 @@ export default {
justify-content: center; justify-content: center;
margin-left: 20px; margin-left: 20px;
margin-top: 5px; margin-top: 5px;
;} }
</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