forked from invegat/Sprint-Challenge-Authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
145 lines (90 loc) · 3.61 KB
/
notes.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
-------------------------------------
Notes on my process, just for fun!
-------------------------------------
- Forked.
- Read Me - Self-Study / Essay Questions
Installing:
- yarn add express
- yarn
- yarn server | Server Listens
-------------------------------------
Implement User Authentication System:
A 1-way process where we take a user password in plain text & translate it into a string that you can no longer reverse back into the password.
- npm install bcryptjs
1. Implement the register and login functionality inside /auth/auth-router.js.
- require bcrypt
- fill in /register and /login
- create a users model (useres > users-model.js) to require in auth-router
- POSTMAN : localhost:3300/api/auth/register
- Test user register : SUCCESS
- Will not allow registration without a password! :)
- Username required!! :)
- POSTMAN : localhost:3300/api/auth/login
- Test User Login : SUCCESS
- SQLITE:
- Sqlite Studio : Password successfully hashed
-------------------------------------
////////////////////
-------------------------------------
Implement the authenticate middleware inside /auth/authenticate-middleware.js.
1. Added code to authenticate credentials.
2. POSTMAN : localhost:3300/api/jokes
- RESULT : "message": "invalid credentials"
- Test /register + /login THEN /api/jokes : Still invalid ??
- Fix:
* Added ./authenticate-middleware.js to auth-router.js
* Also, made sure headers matched new users
* Resolved.
-------------------------------------
////////////////////
Write a minimum of 2 tests per API endpoint.
1. auth > auth-router.js.spec.js
2. Update pckg json :
* "scripts": {
"server": "nodemon index.js",
"test": "cross-env DB_ENV=testing jest --watch --verbose",
"start": "node index.js"
},
3. Dependencies :
- npm install supertest --save-dev
// Something wasn't working when I started 'yarn test' so installing these was a part of troubleshooting to make sure dependencies wasn't the problem:
- npm i --save-dev supertest
- npm install --save-dev cross-env
- npm install dotenv
- npm install knex
- npm install sqlite3 --save
- Add to json :
"jest": {
"testEnvirnoment": "node"
}
- index.js - require('dotenv').config();
- Added testing to knexfile.js
- yarn add --dev jest ~ this was the problem, tests can now run
4. auth-router.js.spec.js
- Describe 1 : Register
2 tests: passed
- Describe 2 : Login
2 tests: passed
5. Forgot about : Use JSON Web Tokens or Sessions and Cookies to persist authentication across requests.
1. npm install jsonwebtoken
2. auth-router.js
* const jwt = require('jsonwebtoken');
* Token code :
- gen token
- add to : /register & /login
* config > secrets.js : 'ilikebread'
* Getting errors in Postman : Error: Illegal arguments
... troubleshooting steps:
- npm install helmet --save
- npm install cors
- npm install router
- Had not selected the content type in Postman, oy!
* Token now works but getting a 401 unauthorized
- Updated authenticate-middleware file
* Test out by:
- Stop server
- Start server
- Get jokes again
- OK good, I can still see jokes, great
*/