Last active
August 15, 2018 06:34
-
-
Save ryzr/bb897bd00967007f6a3ee6c201d19187 to your computer and use it in GitHub Desktop.
Custom Api Authorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::post('register', 'ApiAuthorizationController@register'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.Vue = require('vue'); | |
const app = new Vue({ | |
el: '#app', | |
data: { | |
name: '', | |
email: '', | |
accessToken: localStorage.getItem('accessToken'), | |
}, | |
watch: { | |
accessToken(accessToken) { | |
localStorage.setItem('accessToken', accessToken); | |
axios.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`; | |
}, | |
}, | |
computed: { | |
isLoggedIn() { | |
return !!this.accessToken; | |
}, | |
}, | |
mounted() { | |
if (this.isLoggedIn) { | |
axios.defaults.headers.common['Authorization'] = `Bearer ${this.accessToken}`; | |
this.getUser(); | |
} | |
}, | |
methods: { | |
async getUser() { | |
const response = await axios.get('/api/user'); | |
this.name = response.data.name; | |
this.email = response.data.email; | |
console.log(response); | |
return response; | |
}, | |
async register() { | |
const response = await axios.post('/api/register', { | |
email: this.email, | |
name: this.name, | |
}); | |
this.accessToken = response.data.access_token; | |
this.getUser(); | |
}, | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="{{ app()->getLocale() }}"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<meta name="csrf-token" value="{{ csrf_token() }}"> | |
<title>Laravel</title> | |
<!-- Fonts --> | |
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css"> | |
<link href="/css/app.css" rel="stylesheet" type="text/css"> | |
</head> | |
<body> | |
<div id="app" class="flex-center position-ref full-height"> | |
<div class="content"> | |
<div class="d-flex"> | |
<input @keyup.enter="register" type="text" v-model="name" class="form-control" /> | |
<input @keyup.enter="register" type="email" v-model="email" class="form-control" /> | |
<button @click.prevent="register" class="btn btn-primary">Submit</button> | |
</div> | |
</div> | |
</div> | |
<script src="/js/app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment