To run the app:
- Create a
.envfile with a random cookie secret:
echo SECRET=$(node -e "require('crypto').randomBytes(48, function(ex, buf) { console.log(buf.toString('hex')) });") >> .env- This secret code is used in
app.jsby the cookie-parser module. Uncomment the following line inapp.jsshould be around line 27.
app.use(cookieParser(process.env.SECRET));- Install
npmdependencies and create thepsqldatabase:
npm install
createdb galvanize-form-auth-with-express-knex-pg-bcrypt- Run the
knexmigration (located in the migrations folder) to create the tables on the database:
knex migrate:latest- Start the app:
npm startThe app is hosted on port 3000: http://localhost:3000/
app.js defines the routes in our application.
app.use('/auth', auth);
app.use('/users', users);The auth routes file (./routes/auth.js) contains the following routes:
POST signup
POST signin
GET logoutThe users routes file (./routes/users.js) contains the following routes:
GET /users (lists users)
GET /users/:id (gets a single user)The modular crypt format for bcrypt consists of
$2$,$2a$or$2y$identifying the hashing algorithm and format,- a two digit value denoting the cost parameter, followed by
$ - a 53 characters long base-64-encoded value (they use the alphabet
.,/,0–9,A–Z,a–zthat is different to the standard Base 64 Encoding alphabet) consisting of:- 22 characters of salt (effectively only 128 bits of the 132 decoded bits)
- 31 characters of encrypted output (effectively only 184 bits of the 186 decoded bits)
Thus the total length is 59 or 60 bytes respectively.
Examples:
let hash = bcrypt.hashSync('password', 10);
// '$2a$10$VqeqkeCsxlKfRefRSNZXf.WH5o52XyO3f4wZYAuVd8yGSoZamiT9u'
bcrypt.compareSync('password', hash);
// true
bcrypt.compareSync('wRoNgPaSsWoRd', hash);
// falseThe number 10 in the hashSync example above referes to the number of cycles used to generate the salt. The larger this number, the longer it takes to create the salt, which in theory makes it more secure.
Implement the following features:
- We need
regularusers andadminusers.
- Update the
knex > db/migrationto include a column calledadmin, which is a boolean and defaults tofalse. - The
signupview needs to have an radio input for selecting if the user signing up is an admin or not, it should default tofalse.
- Only logged in users of type
adminare allowed to list all users. [Route/users] this route should return asjson. - A logged in
regularuser can only request their own user id. [Route/users/:id] this route should return asjson.
- If they try to request another user's id, they should be informed they are not an
adminand this route should return asjson.
- An
adminuser can delete a user [Route/users/:id]
- Create a
deleteroute foradminand this route should return asjson. adminview inloggedin: For admins only, theloggedinview will list all users in theuserstable in the view. Each row should have a delete button for removing that specific user.- An
admincan only deleteregularusers. Anadmincannot delete anotheradmin.
- Create a
knexmigration to seed your database with 100 random users of typeadminandregular. - Think of this APP as a simple user management APP and
styleit. Make it pretty. 💚
Oh snap, the last commit was wacky. Issues, with PW salting. Oh no...
- Route status changes
- bcrypt pw hashing updated
- database migration added
Remember to run migrations. Also you're going to have merge conflicts 😄. You need to fix that. Another thing, users in your db probably wont auth anymore so you'll need to delete all rows and re-seed.