Skip to content

Commit 5c198e5

Browse files
committed
user auth module
1 parent 6e47533 commit 5c198e5

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

src/App.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
3030
methods: {
3131
logout() {
32-
api.logout(this,'/login');
32+
api.logout()
33+
.then((response)=>{
34+
this.$router.push('/login');
35+
});
3336
}
3437
}
3538
}

src/components/Dashboard.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
import TodoList from './TodoList'
1414
import CreateTodo from './CreateTodo'
15+
import api from '../utils/api'
1516
1617
export default {
1718
name: 'Dashboard',
@@ -47,7 +48,7 @@ export default {
4748
};
4849
},
4950
beforeMount(){
50-
//this.username = api.user.username;
51+
this.username = api.user.username;
5152
5253
5354
},

src/components/Login.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ export default {
6060
var credentials = {
6161
username: this.credentials.username,
6262
password: this.credentials.password
63-
}
64-
// We need to pass the component's this context
65-
// to properly make use of http in the auth service
63+
}
6664
67-
api.login(this, credentials, '/dashboard');
65+
api.login(credentials)
66+
.then((resp)=>{
67+
this.$router.push('/dashboard'); //redirect to dashboard
68+
})
69+
.catch((error)=>{
70+
this.error = error.response.data;
71+
});
6872
}
6973
}
7074
},

src/components/Register.vue

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
22
<form id="register-form" role="form" style="display: none;">
3+
<div class="alert alert-danger" v-if="error">
4+
<p>{{ error }}</p>
5+
</div>
36
<div class="form-group">
47
<input type="text"required name="username" id="username" tabindex="1" class="form-control" placeholder="Username" value="" v-model="user.username">
58
</div>
@@ -38,7 +41,8 @@
3841
name: '',
3942
email: '',
4043
password: '',
41-
}
44+
},
45+
error: ''
4246
}
4347
},
4448
@@ -57,14 +61,24 @@
5761
e.preventDefault();
5862
api.register(this,this.user,'/dashboard')
5963
.then((response)=>{
60-
var credentials = {
61-
credentials : {
62-
username: this.user.username,
63-
password: this.user.password
64-
}
64+
console.log('register response-> '+ JSON.stringify(response));
65+
var credentials = {
66+
username: this.user.username,
67+
password: this.user.password
6568
};
69+
console.log('register credentials:: ' + JSON.stringify(credentials));
6670
67-
api.login(this,credentials,'/dashboard');
71+
//api.login(this,credentials,'/dashboard');
72+
api.login(credentials)
73+
.then((resp)=>{
74+
this.$router.push('/dashboard'); //redirect to dashboard
75+
})
76+
.catch((error)=>{
77+
this.error = error.response.data;
78+
});
79+
})
80+
.catch((error)=>{
81+
this.error = error.response.data;
6882
});
6983
}
7084

src/utils/api.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ export default {
1111
username:''
1212
},
1313

14-
login(context, creds, redirect) {
15-
axios.post(nodeurl + '/auth/login', creds) //{ username: credentials.username, password: credentials.password }
14+
login(creds) {
15+
return axios.post(nodeurl + '/auth/login', creds) //{ username: credentials.username, password: credentials.password }
1616
.then((response)=>{
1717
console.log('loggedin response '+ JSON.stringify(response));
1818
//login success
1919
this.user.authenticated = true;
2020
this.user.username = response.data.user.username;
21-
context.$router.push(redirect); //redirect to dashboard
21+
//return response;
22+
//context.$router.push(redirect); //redirect to dashboard
2223

23-
})
24-
.catch((error)=>{
25-
console.log('loggedin error response '+ JSON.stringify(error));
26-
if(error.response.status === 401){ //login failed
27-
context.error = 'Login Failed!';
28-
}
2924
});
3025
},
3126

@@ -34,7 +29,7 @@ export default {
3429
.then((response)=>{
3530
if(response.data.user){ //already loggedin
3631
this.user.authenticated = true;
37-
this.user.username = response.data.user.username;
32+
this.user.username = response.data.user._id;
3833
}
3934
else{
4035
this.user.authenticated = false;
@@ -44,13 +39,14 @@ export default {
4439

4540
},
4641

47-
logout(context,redirect){
48-
axios.get(nodeurl + '/auth/logout')
42+
logout(){
43+
return axios.get(nodeurl + '/auth/logout')
4944
.then(function(response){
5045
//if(response.data.message='loggedout'){
5146
console.log(response);
5247
this.user.authenticated = false;
53-
context.$router.push(redirect);
48+
//return response;
49+
//context.$router.push(redirect);
5450
//}
5551
}.bind(this));
5652
},
@@ -85,7 +81,7 @@ export default {
8581
},
8682

8783
register(context, userdetails, redirect){
88-
return axios.post(nodeurl + 'auth/register', userdetails);
84+
return axios.post(nodeurl + '/auth/register', userdetails);
8985
},
9086

9187
}

0 commit comments

Comments
 (0)