FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
angular_node_auth_using_jwt/server.js at master · logesh-kumar/angular_node_auth_using_jwt · GitHub
logesh-kumar
/
angular_node_auth_using_jwt
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
angular_node_auth_using_jwt
/
server.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (48 loc) · 1.28 KB
Breadcrumbs
angular_node_auth_using_jwt
/
server.js
Copy path
File metadata and controls
58 lines (48 loc) · 1.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var
express
=
require
(
'express'
)
;
var
app
=
express
(
)
;
var
faker
=
require
(
'faker'
)
;
var
cors
=
require
(
'cors'
)
;
var
bodyParser
=
require
(
'body-parser'
)
;
var
jwt
=
require
(
'jsonwebtoken'
)
;
var
expressJwt
=
require
(
'express-jwt'
)
;
var
jwtSecret
=
"123456abcd"
;
var
user
=
{
username
:
'logesh'
,
password
:
'r'
}
;
app
.
use
(
cors
(
)
)
;
app
.
use
(
express
.
static
(
__dirname
+
'/public'
)
)
;
app
.
use
(
bodyParser
.
json
(
)
)
;
app
.
use
(
expressJwt
(
{
secret
:
jwtSecret
}
)
.
unless
(
{
path
:
[
'/login'
]
}
)
)
;
app
.
get
(
'/random-user'
,
function
(
req
,
res
)
{
var
user
=
faker
.
helpers
.
userCard
(
)
;
user
.
avatar
=
faker
.
image
.
avatar
(
)
;
res
.
json
(
user
)
;
}
)
;
app
.
post
(
'/login'
,
authenticate
,
function
(
req
,
res
)
{
var
token
=
jwt
.
sign
(
{
username
:
user
.
username
}
,
jwtSecret
)
;
res
.
send
(
{
token
:
token
,
user
:
user
}
)
;
}
)
;
app
.
get
(
'/me'
,
function
(
req
,
res
)
{
res
.
send
(
req
.
user
)
;
}
)
;
//starts a server
app
.
listen
(
3000
,
function
(
)
{
console
.
log
(
'App listening on localhost:3000'
)
;
}
)
;
// UTIL FUNCTIONS
function
authenticate
(
req
,
res
,
next
)
{
var
body
=
req
.
body
;
if
(
!
body
.
username
||
!
body
.
password
)
{
res
.
status
(
400
)
.
end
(
'Must provide username or password'
)
;
}
if
(
body
.
username
!==
user
.
username
||
body
.
password
!==
user
.
password
)
{
res
.
status
(
401
)
.
end
(
'Username or password incorrect'
)
;
}
next
(
)
;
}
Back
|
FazBrowse Home
|
New Git URL