S
S
Spoon in the brain2019-08-28 14:54:46
Flask
Spoon in the brain, 2019-08-28 14:54:46

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>

Flask
@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')

So this is how redirects are made in VueJS, with a FLask backend?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sashqa, 2019-08-28
@vessels

Maybe this will help you Although this is
probably better

D
domanskiy, 2021-01-21
@domanskiy

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>

Those. the method sends the form data to the action.
The Vuex action sends data to the server via an API, and so on.
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)
        },

Maybe not the best code. But so far so. I'll refactor later.
An entry in the localstore, so as not to lose data when the page is reloaded.
Mutation:
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 question

Ask a Question

731 491 924 answers to any question