Logged in/ Logged out

- Created midlewares for authenticated and guest]
- Add the token to the api calls
- Added redirect after the login
This commit is contained in:
2020-11-11 20:26:45 +00:00
parent fe1a5d77c3
commit 6945c27ec0
13 changed files with 597 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
export default function ({ $axios }, inject) {
export default function ({ $axios, store }, inject) {
// Create a custom axios instance
const api = $axios.create({
headers: {
@@ -11,6 +11,28 @@ export default function ({ $axios }, inject) {
// Set baseURL to something different
api.setBaseURL(process.env.baseApiUrl)
api.onRequest(config => {
config.headers = {
...config.headers,
Authorization: store.state.auth.token
}
})
// Inject to context as $api
inject('api', api)
}
// export default function ({ $axios, redirect }) {
// $axios.s
// $axios.onRequest(config => {
// console.log('Making request to ' + config.url)
// })
//
// $axios.onError(error => {
// const code = parseInt(error.response && error.response.status)
// if (code === 400) {
// redirect('/400')
// }
// })
// }

View File

@@ -1,22 +1,54 @@
import { createServer } from 'miragejs'
import { createServer, Model } from 'miragejs'
const gravatar = require('gravatar')
if (process.env.NODE_ENV === 'development') {
startServer(0)
// startServer()
}
function startServer() {
createServer({
models: {
video: Model,
profile: Model,
category: Model
},
seeds(server) {
server.create("video", {title: "Awesome video", thumbnail: "https://images.unsplash.com/photo-1517841905240-472988babdf9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=8&w=1024&h=1024&q=80"})
server.createList("video", 10, {thumbnail: "https://images.unsplash.com/photo-1517841905240-472988babdf9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=8&w=1024&h=1024&q=80"})
server.create("category", {name: "Sports"})
server.createList("category", 21)
},
routes() {
this.namespace = 'api'
this.get('/movies', () => {
return {
movies: [
{ id: 1, name: 'Inception', year: 2010 },
{ id: 2, name: 'Interstellar', year: 2014 },
{ id: 3, name: 'Dunkirk', year: 2017 },
],
}
this.get('/videos', (schema) => {
return schema.videos.all()
})
this.get('/profiles', (schema) => {
return schema.profiles.all()
})
this.get('/profiles/:id', (schema, request) => {
let id = request.params.id
return schema.profiles.find(id)
})
this.post('/profiles', (schema, request) => {
let attrs = JSON.parse(request.requestBody)
attrs.id = schema.profiles.all().length + 1
attrs.email = 'testEmail@yopmail.com'
attrs.picture = gravatar.url(attrs.email)
return { profile: attrs }
})
this.get('/categories', (schema, request) => {
return schema.categories.all()
})
},
})