|
| 1 | +<template> |
| 2 | +<div class="content has-text-centered"> |
| 3 | + <h1 class="is-title is-bold">Login</h1> |
| 4 | + |
| 5 | + <div class="columns is-vcentered"> |
| 6 | + <div class="column is-6 is-offset-3"> |
| 7 | + <div class="box"> |
| 8 | + <div v-show="error" style="color:red; word-wrap:break-word;">{{ error }}</div> |
| 9 | + <form v-on:submit.prevent="login"> |
| 10 | + <label class="label">Email</label> |
| 11 | + <p class="control"> |
| 12 | + <input v-model="data.body.username" class="input" type="text" placeholder="email@example.org"> |
| 13 | + </p> |
| 14 | + <label class="label">Password</label> |
| 15 | + <p class="control"> |
| 16 | + <input v-model="data.body.password" class="input" type="password" placeholder="password"> |
| 17 | + </p> |
| 18 | + |
| 19 | + <p class="control"> |
| 20 | + <label class="checkbox"> |
| 21 | + <input type="checkbox" v-model="data.rememberMe"> |
| 22 | + Remember me |
| 23 | + </label> |
| 24 | + </p> |
| 25 | + |
| 26 | + <hr> |
| 27 | + <p class="control"> |
| 28 | + <button type="submit" class="button is-primary">Login</button> |
| 29 | + <button class="button is-default">Cancel</button> |
| 30 | + </p> |
| 31 | + </form> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | +</div> |
| 36 | +</template> |
| 37 | + |
| 38 | +<script> |
| 39 | +export default { |
| 40 | +
|
| 41 | + data () { |
| 42 | + return { |
| 43 | + data: { |
| 44 | + body: { |
| 45 | + username: null, |
| 46 | + password: null |
| 47 | + }, |
| 48 | + rememberMe: false |
| 49 | + }, |
| 50 | + error: null |
| 51 | + } |
| 52 | + }, |
| 53 | + mounted () { |
| 54 | + if (this.$auth.redirect()) { |
| 55 | + console.log('Redirect from: ' + this.$auth.redirect().from.name) |
| 56 | + } |
| 57 | + // Can set query parameter here for auth redirect or just do it silently in login redirect. |
| 58 | + }, |
| 59 | + methods: { |
| 60 | + login () { |
| 61 | + var redirect = this.$auth.redirect() |
| 62 | + this.$auth.login({ |
| 63 | + headers: { |
| 64 | + 'Content-Type': 'application/json' |
| 65 | + }, |
| 66 | + data: this.data.body, |
| 67 | + rememberMe: this.data.rememberMe, |
| 68 | + redirect: {name: redirect ? redirect.from.name : 'Home'}, |
| 69 | + success (res) { |
| 70 | + console.log('Auth Success') |
| 71 | + // console.log('Token: ' + this.$auth.token()) |
| 72 | + // console.log(res) |
| 73 | + }, |
| 74 | + error (err) { |
| 75 | + if (err.response) { |
| 76 | + // The request was made, but the server responded with a status code |
| 77 | + // that falls out of the range of 2xx |
| 78 | + // console.log(err.response.status) |
| 79 | + // console.log(err.response.data) |
| 80 | + // console.log(err.response.headers) |
| 81 | + this.error = err.response.data |
| 82 | + } else { |
| 83 | + // Something happened in setting up the request that triggered an Error |
| 84 | + console.log('Error', err.message) |
| 85 | + } |
| 86 | + console.log(err.config) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | + } |
| 91 | + // filters: { |
| 92 | + // json: function (value) { |
| 93 | + // console.log(value) |
| 94 | + // return value |
| 95 | + // } |
| 96 | + // } |
| 97 | +
|
| 98 | +} |
| 99 | +</script> |
| 100 | + |
| 101 | +<style lang="scss" scoped> |
| 102 | +.is-title { |
| 103 | + text-transform: capitalize; |
| 104 | +} |
| 105 | +</style> |
0 commit comments