Skip to content

Commit e21dfbb

Browse files
committed
Criado rotas para insercao e seguranca de acesso de rotas
1 parent a95ed14 commit e21dfbb

File tree

8 files changed

+102
-14
lines changed

8 files changed

+102
-14
lines changed

client/config/dev.env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ const prodEnv = require('./prod.env')
55

66
module.exports = merge(prodEnv, {
77
NODE_ENV: '"development"',
8-
SERVER: '"http://localhost:9091"'
8+
SERVER: '"http://localhost:3000"'
99
})

client/src/components/auth/Login.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<form @submit.prevent="userLogin()">
88
<div class="row">
99
<div class="input-field col s12">
10-
<input id="email" type="email" v-model="login.username" class="validate" />
10+
<input id="email" type="email" v-model="login.user_name" class="validate" />
1111
<label for="email">Email</label>
1212
</div>
1313
</div>
@@ -18,8 +18,7 @@
1818
</div>
1919
</div>
2020
<button class="btn" type="submit">
21-
Acessar
22-
<i class="material-icons rigth">send</i>
21+
Acessar <i class="material-icons rigth">send</i>
2322
</button>
2423
</form>
2524
</div>
@@ -76,10 +75,14 @@ export default {
7675
},
7776
methods: {
7877
userLogin () {
79-
console.log('login')
78+
this.$store.dispatch('authentication', this.login).then(() => {
79+
this.$router.push({path: '/'})
80+
})
8081
},
8182
userRegister () {
82-
console.log('register')
83+
this.$store.dispatch('register', this.register).then(() => {
84+
this.$router.push({path: '/'})
85+
})
8386
}
8487
}
8588
}

client/src/router/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import Vue from 'vue'
22
import Router from 'vue-router'
33
import Hello from '@/components/Hello'
44
import Login from '@/components/auth/Login'
5+
import store from '@/states'
56

67
Vue.use(Router)
78

8-
export default new Router({
9+
let router = new Router({
910
routes: [
1011
{
1112
path: '/',
1213
name: 'Hello',
13-
component: Hello
14+
component: Hello,
15+
meta: {requiresAuth: true}
1416
},
1517
{
1618
path: '/login',
@@ -19,3 +21,25 @@ export default new Router({
1921
}
2022
]
2123
})
24+
25+
export default router
26+
27+
router.beforeEach((to, from, next) => {
28+
let requiresAuth = to.meta.requiresAuth || false
29+
30+
let token = store.state.user.token
31+
32+
if (token) {
33+
window.axios.defaults.headers.common['Authorization'] = 'bearer ' + token
34+
}
35+
36+
if (requiresAuth) {
37+
return store.dispatch('getCurrentUser')
38+
.then(() => {
39+
return next()
40+
}).catch(() => {
41+
return next({path: 'login'})
42+
})
43+
}
44+
return next()
45+
})

client/src/states/modules/user.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
export default {}
1+
let qs = require('qs')
2+
3+
export default {
4+
state: {
5+
me: null,
6+
token: window.localStorage.getItem('token')
7+
},
8+
mutations: {
9+
updateUser (state, data) {
10+
state.me = data
11+
},
12+
updateToken (state, data) {
13+
state.token = data
14+
}
15+
16+
},
17+
actions: {
18+
getCurrentUser (context) {
19+
console.log('context', context)
20+
return window.axios.get('/users/me').then((response) => {
21+
console.log('response do getCurrente', response)
22+
context.commit('updateUser', response.data)
23+
return response
24+
})
25+
},
26+
authentication (context, user) {
27+
return window.axios.post('/users/token', qs.stringify(user)).then((response) => {
28+
context.commit('updateToken', response.date.token)
29+
window.localStorage.setItem('token', response.data.token)
30+
return response
31+
})
32+
},
33+
register (context, user) {
34+
return window.axios.post('/users/register', qs.stringify(user)).then((response) => {
35+
let authData = {
36+
username: user.email,
37+
password: user.password
38+
}
39+
return context.dispatch('authentication', authData)
40+
})
41+
}
42+
}
43+
}

server/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ let path = require('path');
44
let cookieParser = require('cookie-parser');
55
let logger = require('morgan');
66
let mongoose = require('./src/db/connection');
7+
let cors = require('cors');
78

89
let indexRouter = require('./routes/index');
910
let usersRouter = require('./routes/users');
1011

1112
let app = express();
13+
app.use(cors());
1214

1315
// view engine setup
1416
app.set('views', path.join(__dirname, 'views'));

server/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"cookie-parser": "~1.4.4",
10+
"cors": "^2.8.5",
1011
"debug": "~2.6.9",
1112
"ejs": "~2.6.1",
1213
"express": "~4.16.1",

server/routes/users.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ let cfg = require('../config');
77

88
router.post('/token', function (req, res, next) {
99
let user = req.body;
10-
10+
console.log('user',user.user_name)
1111
if (!user.user_name || !user.password) {
12-
return res.status(401).send('Unauthorized');
12+
return res.status(401).send('Unauthorized111');
1313
}
1414

1515
let query = { email: user.user_name, password: user.password };
@@ -20,7 +20,7 @@ router.post('/token', function (req, res, next) {
2020
}
2121

2222
if (!user) {
23-
return res.status(401).send('Unauthorized');
23+
return res.status(401).send('Unauthorized2222');
2424
}
2525

2626
let payload = { id: user.id };
@@ -36,13 +36,15 @@ router.post('/token', function (req, res, next) {
3636
});
3737

3838
router.get('/me', passport.authenticate('jwt', { session: false }), function (req, res, next) {
39+
console.log('reqreq',req)
3940
res.status(200).json({
4041
user: req.user
4142
})
4243

4344
});
4445

45-
router.get('/register', function (req, res, next) {
46+
router.post('/register', function (req, res, next) {
47+
4648
let data = {
4749
name: req.body.name,
4850
email: req.body.email,
@@ -58,7 +60,7 @@ router.get('/register', function (req, res, next) {
5860
if (err) {
5961
return res.status(422).json({ err: err });
6062
}
61-
return res, status(200).json({ user: user });
63+
return res.status(200).json({ user: user });
6264

6365
};
6466

0 commit comments

Comments
 (0)