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>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle> <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse id="nav-collapse" is-nav> <b-collapse id="nav-collapse" is-nav>
<b-navbar-nav> <b-navbar-nav>
<b-nav-item href="#">Link</b-nav-item> <b-nav-item href="#">Link</b-nav-item>
<b-nav-item href="#" disabled>Disabled</b-nav-item> <b-nav-item href="#" disabled>Disabled</b-nav-item>
</b-navbar-nav> </b-navbar-nav>
<!-- 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"
</b-nav-form> 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-item-dropdown text="Lang" right> <b-nav-item-dropdown text="Lang" right>
<b-dropdown-item href="#">EN</b-dropdown-item> <b-dropdown-item href="#">EN</b-dropdown-item>
<b-dropdown-item href="#">ES</b-dropdown-item> <b-dropdown-item href="#">ES</b-dropdown-item>
<b-dropdown-item href="#">RU</b-dropdown-item> <b-dropdown-item href="#">RU</b-dropdown-item>
<b-dropdown-item href="#">FA</b-dropdown-item> <b-dropdown-item href="#">FA</b-dropdown-item>
</b-nav-item-dropdown> </b-nav-item-dropdown>
<b-nav-item-dropdown right> <b-nav-item-dropdown right>
<!-- Using 'button-content' slot --> <!-- Using 'button-content' slot -->
<template #button-content> <template #button-content>
<em>User</em> <em>User</em>
</template> </template>
<b-dropdown-item href="#">Profile</b-dropdown-item> <b-dropdown-item href="#">Profile</b-dropdown-item>
<b-dropdown-item href="#" @click="logout">Sign Out</b-dropdown-item> <b-dropdown-item href="#" @click="logout">Sign Out</b-dropdown-item>
</b-nav-item-dropdown> </b-nav-item-dropdown>
</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,19 +73,40 @@ export default { ...@@ -94,19 +73,40 @@ 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'
// }
}, },
}, },
}; };
</script> </script>
......
<template> <template>
<div class="container" <div
style=" class="container"
display: flex; style="
justify-content: center; display: flex;
align-items: center; justify-content: center;
height: 100vh; align-items: center;
"> 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"
<label>Email :</label> class="form-control"
<input type="email" class="form-control" placeholder="name"
placeholder="Email" v-model="email" required> v-model="name"
max="255"
<label>Password :</label> min="1"
<input type="password" required
class="form-control" />
placeholder="password" v-model="password" required> <label>Email :</label>
<label>Confirm Password :</label> <input
<input type="password" type="email"
class="form-control" class="form-control"
placeholder="Confirm_password" required> placeholder="Email"
<div class="button"> v-model="email"
<button class="w-75 btn btn-lg btn-primary" type="submit">Sign up here</button> required
</div> />
</form> <label>Password :</label>
</div> <input
</div> type="password"
</template> class="form-control"
placeholder="password"
v-model="password"
required
/>
<label>Confirm Password :</label>
<input
type="password"
class="form-control"
placeholder="Confirm_password"
required
/>
<div class="button">
<button class="w-75 btn btn-lg btn-primary" type="submit">
Sign up here
</button>
</div>
</form>
</div>
</div>
</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("/");
}, },
}, },
}; };
...@@ -75,9 +97,9 @@ export default { ...@@ -75,9 +97,9 @@ export default {
font-weight: 600; font-weight: 600;
} }
.btn { .btn {
align-items: center; align-items: center;
justify-content: center; justify-content: center;
margin-left: 20px; margin-left: 20px;
margin-top: 5px; margin-top: 5px;
;} }
</style> </style>
\ No newline at end of file
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