I need to replace all of the authentication code that is in this file with bette
ID: 3749968 • Letter: I
Question
I need to replace all of the authentication code that is in this file with better, Vue authentication. Not going to be storing the username or pass but instead as a JSON. This is my code:
const API_URL = 'http://localhost:8000/'
const LOGIN_URL = API_URL + 'auth/login'
export default {
user: {
authenticated: false
},
login (context, creds, redirect) {
axios({
method: 'POST',
url: LOGIN_URL,
withCredentials: false,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
data: creds
}).then(
resp => {
localStorage.setItem('access_token', resp.data.access_token)
this.user.authenticated = true
if (redirect) {
Router.go(redirect)
}
},
err => {
context.error = err.message
}
)
},
logout () {
localStorage.removeItem('access_token')
this.user.authenticated = false
},
checkAuth () {
var jwt = localStorage.getItem('access_token')
if (jwt) {
this.user.authenticated = true
} else {
this.user.authenticated = false
}
}
}
*****************************************************************************
I need to use this code to replace the Auth Part.
Explanation / Answer
// You can do this. It will work.
const API_URL = 'http://localhost:8000/'
const LOGIN_URL = API_URL + 'auth/login'
export default {
user: {
authenticated: false
},
if (redirect) {
Router.go(redirect)
}
},
err => {
context.error = err.message
}
)
},
logout () {
localStorage.removeItem('access_token')
this.user.authenticated = false
},
}