Browse Source

update login workflow

pull/342/head
Guillaume Vincent 8 years ago
parent
commit
1feeb3a584
4 changed files with 26 additions and 51 deletions
  1. +5
    -5
      src/app/Login.vue
  2. +1
    -1
      src/landing-page/LoginBar/LoginBar.vue
  3. +11
    -11
      src/router.js
  4. +9
    -34
      src/services/auth.js

+ 5
- 5
src/app/Login.vue View File

@@ -52,12 +52,12 @@
password: this.credentials.password, password: this.credentials.password,
}; };
auth.login(credentials) auth.login(credentials)
.then(() => {
.then((data) => {
console.log('Request succeeded with JSON response', data);
router.go('/app/'); router.go('/app/');
})
.catch(() => {
logging.error(this.$t('login.credentials_invalids'));
});
}).catch((error) => {
console.log('Request failed', error);
});
}, },
}, },
}; };


+ 1
- 1
src/landing-page/LoginBar/LoginBar.vue View File

@@ -20,7 +20,7 @@
<div class="container"> <div class="container">
<nav class="nav nav-inline pull-right"> <nav class="nav nav-inline pull-right">
<a class="nav-link" v-bind:class="{ 'bg-primary': $route.path=='/login/'}" <a class="nav-link" v-bind:class="{ 'bg-primary': $route.path=='/login/'}"
v-link="{ path: '/login/' }">
v-link="{ path: '/app/' }">
<i class="fa fa-lock"></i> {{ $t('login.login') }} <i class="fa fa-lock"></i> {{ $t('login.login') }}
</a> </a>
<a class="nav-link" v-bind:class="{ 'bg-primary': $route.path=='/register/' || $route.path=='/'}" <a class="nav-link" v-bind:class="{ 'bg-primary': $route.path=='/register/' || $route.path=='/'}"


+ 11
- 11
src/router.js View File

@@ -4,8 +4,10 @@ import App from './App';


Vue.use(Router); Vue.use(Router);


const router = new Router();

const router = new Router({
history: true,
hashbang: false,
});


import LandingPage from './landing-page/LandingPage'; import LandingPage from './landing-page/LandingPage';
import LoginPage from './app/Login'; import LoginPage from './app/Login';
@@ -14,8 +16,7 @@ import LessPassConnected from './app/Index';


router.map({ router.map({
'/': { '/': {
auth: true,
component: LessPassConnected,
component: LandingPage,
}, },
'/login/': { '/login/': {
component: LoginPage, component: LoginPage,
@@ -23,8 +24,9 @@ router.map({
'/register/': { '/register/': {
component: RegisterPage, component: RegisterPage,
}, },
'/welcome/': {
component: LandingPage,
'/app/': {
auth_required: true,
component: LessPassConnected,
}, },
}); });


@@ -39,13 +41,11 @@ import Auth from './services/auth';
Auth.checkAuth(); Auth.checkAuth();


router.beforeEach(transition => { router.beforeEach(transition => {
alert(transition.to.auth);
if (transition.to.auth && !Auth.user.authenticated) {
alert(Auth.user.authenticated);
transition.redirect('/welcome/');
if (transition.to.auth_required && !Auth.user.authenticated) {
transition.redirect('/login/');
} else { } else {
transition.next(); transition.next();
} }
}); });


module.exports = router;
export default router;

+ 9
- 34
src/services/auth.js View File

@@ -1,5 +1,3 @@
import logging from './logging';

function checkStatus(response) { function checkStatus(response) {
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
return response; return response;
@@ -17,6 +15,7 @@ module.exports = {
user: { user: {
authenticated: false, authenticated: false,
}, },

login(credential) { login(credential) {
return fetch('/api/sessions/', { return fetch('/api/sessions/', {
method: 'post', method: 'post',
@@ -26,38 +25,12 @@ module.exports = {
}, },
body: JSON.stringify(credential), body: JSON.stringify(credential),
}).then(checkStatus) }).then(checkStatus)
.then(parseJSON);
},
login2(context, credentials, callback) {
const self = this;

fetch('/users.html');
context.$http.post('/api/sessions/', credentials).then(
response => {
localStorage.setItem('token', response.data.token);
self.user.authenticated = true;
logging.success(this.$t('login.welcome'));
if (callback) {
callback();
}
},
() => {
logging.error(this.$t('login.credentials_invalids'));
}
);
},

register(context, user, callback) {
context.$http.post('/api/users/', user).then(
() => {
if (callback) {
callback();
}
},
() => {
logging.warning(this.$t('register.beta'));
}
);
.then(parseJSON)
.then((data) => {
localStorage.setItem('token', data.token);
this.user.authenticated = true;
return data;
});
}, },


logout(callback) { logout(callback) {
@@ -69,7 +42,9 @@ module.exports = {
}, },


checkAuth() { checkAuth() {
console.log('check auth');
const jwt = localStorage.getItem('token'); const jwt = localStorage.getItem('token');
console.log(jwt);
this.user.authenticated = !!jwt; this.user.authenticated = !!jwt;
}, },




Loading…
Cancel
Save