Answer the question
In order to leave comments, you need to log in
How to redirect VueJS & Flask?
Good afternoon, I want to redirect to a specific page after authorization, here is the code:
VueJS:
<template>
<div class="container">
<form>
<input type="text" name="login" v-model="login" placeholder="login">
<input type="text" name="pass" v-model="pass" placeholder="pass">
<button @click="loginSubmit">Submit</button>
</form>
</div>
</template>
<script>
var Axios = require('axios')
export default{
data: function(){
return{
login: '',
pass: ''
}
},
methods: {
loginSubmit: function(e){
e.preventDefault()
Axios.post('http://192.168.1.102:5000/authorization', {login: this.login, pass: this.pass})
.then(response => { })
}
}
}
</script>
@app.route('/authorization', methods = ['GET', 'POST'])
def authorization():
if request.method == 'POST':
user = request.get_json()
dump = json.dumps(user)
data = json.loads(dump)
if data['login'] == 'admin':
#Тут я не совсем понимаю что писать,
#что передавать в VueJS чтобы произошёл редирект?
return render_template('index.html')
Answer the question
In order to leave comments, you need to log in
At the very moment, the project is a training RestAPI on Flask + Vue.
You need to give JSON in the return and code 200 if everything is OK
In JSON, shove the token, username, user role, etc.
Token, using Vuex to shove into the stait and use in requests
I use Vuex. I advise
Pro redirect.
<script>
import {mapGetters, mapActions} from 'vuex'
export default {
name: 'login',
data() {
return {
form: {
email: null,
password: null,
}
}
},
// computed: mapGetters(['user/USER_AUTH']),
methods: {
...mapActions({
loginUser: 'user/LOGIN_USER'
}),
submit() {
this.loginUser(this.form)
.then(() => this.$router.push('/'))
.catch(err => console.log(err))
}
}
}
</script>
actions: {
async LOGIN_USER({ commit }, form) {
const response = await axios.post('api/v1/login', form)
const user = await response.data
commit('UPDATE_USER_AUTH', user)
},
mutations: {
UPDATE_USER_AUTH(state, user) {
localStorage.token = user.token
localStorage.email = user.email
localStorage.name = user.name
localStorage.role = user.role
state.token = user.token
state.email = user.email
state.name = user.name
state.role = user.role
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question