Skip to content

Commit 6effd1a

Browse files
committed
deleted duplicate files
1 parent 6a46fec commit 6effd1a

File tree

8 files changed

+145
-66
lines changed

8 files changed

+145
-66
lines changed

_server.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var express = require('express')
2+
var jwt = require('jsonwebtoken')
3+
var cors = require('cors')
4+
var bodyParser = require('body-parser')
5+
var fs = require('fs')
6+
var events = require('./db/events.json')
7+
8+
const app = express()
9+
10+
app.use(cors())
11+
app.use(bodyParser.json())
12+
13+
app.get('/', (req, res) => {
14+
res.json({
15+
message: 'Welcome to the API.'
16+
})
17+
})
18+
19+
app.get('/dashboard', verifyToken, (req, res) => {
20+
jwt.verify(req.token, 'the_secret_key', err => {
21+
if (err) {
22+
res.sendStatus(401)
23+
} else {
24+
res.json({
25+
events: events
26+
})
27+
}
28+
})
29+
})
30+
31+
app.post('/register', (req, res) => {
32+
if (req.body) {
33+
const user = {
34+
name: req.body.name,
35+
email: req.body.email,
36+
password: req.body.password
37+
// You'll want to encrypt the password in a live app
38+
}
39+
40+
var data = JSON.stringify(user, null, 2)
41+
42+
fs.writeFile('db/user.json', data, err => {
43+
if (err) {
44+
console.log(err)
45+
} else {
46+
console.log('Added user to user.json')
47+
}
48+
})
49+
// The secret key should be an evironment variable in a live app
50+
const token = jwt.sign({ user }, 'the_secret_key')
51+
res.json({
52+
token,
53+
email: user.email,
54+
name: user.name
55+
})
56+
} else {
57+
res.sendStatus(401)
58+
}
59+
})
60+
61+
app.post('/login', (req, res) => {
62+
var userDB = fs.readFileSync('./db/user.json')
63+
var userInfo = JSON.parse(userDB)
64+
if (
65+
req.body &&
66+
req.body.email === userInfo.email &&
67+
req.body.password === userInfo.password
68+
) {
69+
// The secret key should be an environment variable in a live app
70+
const token = jwt.sign({ userInfo }, 'the_secret_key')
71+
res.json({
72+
token,
73+
email: userInfo.email,
74+
name: userInfo.name
75+
})
76+
} else {
77+
res.sendStatus(401)
78+
}
79+
})
80+
81+
function verifyToken (req, res, next) {
82+
const bearerHeader = req.headers['authorization']
83+
84+
if (typeof bearerHeader !== 'undefined') {
85+
const bearer = bearerHeader.split(' ')
86+
const bearerToken = bearer[1]
87+
req.token = bearerToken
88+
next()
89+
} else {
90+
res.sendStatus(401)
91+
}
92+
}
93+
94+
app.listen(3000, () => {
95+
console.log('Server started on port 3000')
96+
})

db/user.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "adam",
3-
"email": "adam@gmail.com",
2+
"name": "Mary Beth",
3+
"email": "mary@gmail.com",
44
"password": "pass123"
55
}

package-lock.json

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

src/App.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
import AppNav from './components/AppNav'
1010
1111
export default {
12-
name: 'App',
1312
components: { AppNav }
1413
}
1514
</script>
1615

1716
<style lang="scss">
18-
@import "./assets/styles/global.scss";
19-
20-
.page{
17+
@import './assets/styles/global.scss';
18+
.page {
2119
display: flex;
2220
justify-content: center;
2321
flex-direction: column;

src/components/AppNav.vue

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
<template>
22
<div id="nav">
33
<router-link to="/">
4-
Home
5-
</router-link>
4+
Home
5+
</router-link>
66

77
<template v-if="user">
88
<router-link to="dashboard">
9-
Dashboard
10-
</router-link>
9+
Dashboard
10+
</router-link>
1111

1212
<span class="nav-welcome">Hello, {{ user.name }}.</span>
1313

14-
<button type="button" @click="logout">
15-
Log out
16-
</button>
14+
<button type="button" class="logoutButton" @click="logout">
15+
Log out
16+
</button>
1717
</template>
1818

1919
<template v-else>
2020
<router-link to="authenticate" class="button">
21-
Login
22-
</router-link>
21+
Login
22+
</router-link>
2323
</template>
2424
</div>
2525
</template>
2626

2727
<script>
2828
export default {
29-
name: 'AppNav',
3029
computed: {
31-
user () { return this.$store.state.user }
30+
user () {
31+
return this.$store.state.user
32+
}
3233
},
3334
methods: {
34-
logout () { this.$store.dispatch('logout') }
35+
logout () {
36+
this.$store.dispatch('logout')
37+
}
3538
}
3639
}
3740
</script>
@@ -77,6 +80,10 @@ button,
7780
}
7881
}
7982
83+
.logoutButton {
84+
cursor: pointer;
85+
}
86+
8087
.nav-welcome + button {
8188
margin-left: 0;
8289
}

src/server.js

Whitespace-only changes.

src/store.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Vue.use(Vuex)
77
export default new Vuex.Store({
88
state: {
99
user: null,
10-
status: null,
1110
isNewUser: true
1211
},
1312
mutations: {
@@ -18,14 +17,11 @@ export default new Vuex.Store({
1817
}`
1918
state.user = userData
2019
},
21-
SET_STATUS (state, status) {
22-
state.status = status
23-
},
2420
LOGOUT () {
2521
localStorage.removeItem('user')
2622
location.reload()
2723
},
28-
ISNEWUSER (state, isNewUser) {
24+
IS_NEW_USER (state, isNewUser) {
2925
state.isNewUser = isNewUser
3026
}
3127
},
@@ -48,7 +44,7 @@ export default new Vuex.Store({
4844
commit('LOGOUT')
4945
},
5046
isNewUser ({ commit }, isNewUser) {
51-
commit('ISNEWUSER', isNewUser)
47+
commit('IS_NEW_USER', isNewUser)
5248
}
5349
}
5450
})

0 commit comments

Comments
 (0)