Hello everybody. I do authentication on nakst + django. When you try to log in, the page freezes. What could be the problem? Thanks
login.vue
<template>
<el-card shadow="always" :style="{ width: '500px' }">
<el-form
:model="userData"
:rules="rules"
ref="form"
@submit.native.prevent="logInUser"
>
<h2>Login</h2>
<el-form-item label="Email" prop="email">
<el-input v-model.trim="userData.email" />
</el-form-item>
<div class="mb2">
<el-form-item label="Пароль" prop="password">
<el-input
v-model.trim="userData.password"
type="password"
/>
</el-form-item>
</div>
<el-form-item>
<el-button
type="primary"
native-type="submit"
round
:loading="loading"
@click="logInUser(userData)"
>
Войти
</el-button>
</el-form-item>
</el-form>
</el-card>
</template>
<script>
export default {
layout: "empty",
middleware: ['admin-auth'],
data() {
return {
loading: false,
userData: {
email: '',
password: '',
},
rules: {
email: [
{
required: true,
message: "Введите email",
trigger: "blur",
},
],
password: [
{
required: true,
message: "Введите пароль",
trigger: "blur",
},
{
min: 6,
message: "Пароль должен быть не менее 6 символов",
trigger: "blur",
},
],
},
};
},
methods: {
async logInUser(userData) {
this.$refs.form.validate(async (valid) => {
if (valid) {
this.loading = true;
try {
await this.$auth.loginWith('local', {
data: userData,
})
this.$message('Login!!!!!!!!!!!!!!')
this.$router.push("/admin/")
console.log('notification successful')
} catch (error) {
this.$message({
message: 'Warning, this is a warning message.',
type: 'warning'
});
console.log('notification unsuccessful')
this.$router.push("/login"),
this.loading = false;
}
console.log(this.$auth.user)
}
});
},
},
};
</script>
middleware/admin-auth.js
export default function ({ store, redirect }) {
if (store.state.auth.loggedIn) {
return redirect('/admin/')
}
if (!store.state.auth.loggedIn) {
return redirect('/login')
}
}
store/index.js
export const getters = {
isAuthenticated(state) {
return state.auth.loggedIn
},
loggedInUser(state) {
return state.auth.user
}
}
nuxt.config.js
export default {
target: 'server',
head: {
title: 'nuxt-blog',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
css: [
'element-ui/lib/theme-chalk/index.css',
'@theme/index.scss'
],
plugins: [
'@/plugins/globals'
],
components: true,
buildModules: [
],
modules: [
'@nuxtjs/axios',
'@nuxtjs/auth',
],
axios: {
baseURL: 'http://127.0.0.1:8000/'
},
build: {
transpile: [/^element-ui/],
},
auth: {
strategies: {
local: {
endpoints: {
login: {
url: 'token/login/',
method: 'post',
propertyName: 'auth_token',
},
logout: { url: 'token/logout/', method: 'post' },
user: {
url: 'accounts/data/',
method: 'get',
propertyName: false,
},
},
tokenType: 'Token',
tokenName: 'Authorization',
},
redirect: {
register: '/register',
login: '/login',
home: '/',
},
},
}
}