know_youre_real v1.2.0
know_youre_real
Problem Statement:
- Difficult to integrate login and signup securely and convenient
- Maybe check devices -> does the device match?
How to use - less than 10 steps in implementing authentication (given familiarity with pg and knex)
- Create knex migrations with user and device table Both user and device tables require these following values:
User table
table.increments("id").primary();
table.string("email").unique();
table.boolean("logged_in")
table.string("hash");
Device Table
table.increments("id").primary();
table.integer("user_id").unsigned().references("user.id").onUpdate("CASCADE").onDelete("CASCADE")
table.string("type")
table.string("device")
table.timestamp("last_login")
table.timestamp("created").defaultTo(knex.fn.now());
Run migrations
For post signup route
- Checks if email already exists
- Checks if email is valid
- Changes password to hash
- Adds device
- Returns user object
const kyr = require("know_youre_real")
const {see, hourglass} = require("code_clarity")
app.post("/api/signup/", async(req, res) => {
let postAndGetUser = await kyr.signup(req, knex, req.body)
// returns a user object that you can send to the frontend
if (postAndGetUser.error) {
see.problem(postAndGetUser.error)
see.problem(postAndGetUser.location)
see.should(postAndGetUser.expected)
// handle error
} else {
res.render("dashboard", {user: postAndGetUser})
}
})
- For post login route
- Transforms hash to password
- Changes logged_in in user table to true
- Changes last logged in date in device table
const kyr = require("know_youre_real")
const {see, hourglass} = require("code_clarity")
app.post("/api/login/", async(req, res) => {
let postAndGetUser = await login(req, knex, req.body)
// returns a user object that you can send to the frontend
if (postAndGetUser.error) {
see.problem(postAndGetUser.error)
see.problem(postAndGetUser.location)
see.should(postAndGetUser.expected)
// handle error
} else {
res.render("dashboard", {user: postAndGetUser})
}
})
- For logout
const kyr = require("know_youre_real")
const {see, hourglass} = require("code_clarity")
app.post("/api/logout/:user_id", async(req, res) => {
let id = parseInt(req.params.user_id)
let getBoolean = await kyr.logout(id)
if (getBoolean === true) {
// redirect to home
} else {
// redirect to error page
}
})
- And for every subsequent route
- Checks if device login is still in its verification period (last login + 14 days)
- If user is currently logged in, which are only changeable via signup/login functions (which it checks in the user table)
const kyr = require("know_youre_real")
const {see, hourglass} = require("code_clarity")
app.post("/dashboard/", async(req, res) => {
let getVerified = await kyr.verifyUserRoute(req, knex, user_id, 14)
// returns a user object that you can send to the frontend
if (getVerified.error) {
see.problem(postAndGetUser.error)
see.problem(postAndGetUser.location)
see.should(postAndGetUser.expected)
// handle error
} else {
// redirect to dashboard
}
})
Frontend
axios.post(userObject).then((response) => {
if (response.error) {
} else {
let userObject = kyr.confirm_signup_login(response, "/dashboard")
}
})
- Pull from local storage
// will return id
let id = kyr.verify_local_storage()
axios.post(`/api/user/${id}/task`, object).then((response) => {
})
- Redirect to another page after logout Logout
Example
let sampleObject = {
email: "lesleyc.2@gmail.com",
password: "testtest"
}
let firstSignup = await signup(sampleRequest, knex, sampleObject)
console.log("🚀 ~ file: routes.js ~ line 210 ~ testHandlePost ~ firstSignup", firstSignup)
let firstLogin = await login(sampleRequest, knex, sampleObject)
console.log("🚀 ~ file: routes.js ~ line 194 ~ testHandlePost ~ firstLogin", firstLogin)
let verify = await verifyUserRoute(sampleRequest, knex, firstLogin.id, 14)
console.log("🚀 ~ file: routes.js ~ line 238 ~ testHandlePost ~ verify", verify)
let thenLogout = await logout(knex, firstLogin.id)
console.log("🚀 ~ file: routes.js ~ line 284 ~ testHandlePost ~ thenLogout", thenLogout)
Frontend
<script src="./node_modules/know_youre_real/dist/frontend.js"></script>
<script>
$(() => {
$(".signup").click(function(event) {
let user = confirm_signup_login({
id: 4,
email: "whatsup"
}, "/")
console.log(user)
})
$(".verify").click(function(event) {
let verify = verify_local_storage()
console.log(verify)
})
$(".logout").click(function(event) {
confirm_logout("/")
})
})
</script>
Instructions:
npm install know_youre_real
const kyr = require('know_youre_real')
If utilizing in html...:
<script src="./node_modules/know_youre_real/index.js"></script>
const output = know_youre_real.method(parameter)
Functions
passwordToHash(password) ⇒ string
passwordToHash
Kind: global function
Returns: string - hash
Date: 2022-03-11
Author: zen-out
Param | Type |
---|---|
password | any |
hashToPassword(password, hash) ⇒ boolean
Kind: global function
Date: 2022-03-11
Author: zen-out
Param | Type |
---|---|
password | string |
hash | string |
postDevice(knex, object) ⇒ any
- Get device
- If device exists
- Update the last login date
- Else, create new device
- Return device
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
knex | any |
object | any |
Example
userObject["user_id"] = postUser.id
userObject["device"] = req.device.parser.useragent.source;
userObject["type"] = req.device.type;
let getPost = await postDevice(knex, userObject)
signup(knex, userObject) ⇒ any
- Will grab user from user table
- If user exists, will return error object
- Otherwise, will change logged_in to true
- Will set last_login to today's date.
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
knex | any |
userObject | any |
Example
let firstSignup = await signup(sampleRequest, knex, sampleObject)
login(knex, userObject) ⇒ any
- Transforms hash to password
- Changes logged_in in user table to true
- Changes last logged in date in device table
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
knex | any |
userObject | any |
Example
// let firstLogin = await login(sampleRequest, knex, sampleObject)
verifyUserRoute(req, res, next) ⇒ any
- Checks if user is logged in
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
req | any |
res | any |
next | any |
Example
// let verify = await verifyUserRoute(sampleRequest, knex, 1)
logout(user_id) ⇒ any
- Checks if user is logged in
- If they are, change logged in to false
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
knex | database |
user_id | any |
Example
let thenLogout = await logout(knex, 1) // returns true or false;
Functions
confirm_signup_login(responseFromLoginSignup, route) ⇒ object
- Will stringify parameter
- Set local storage "user" to stringed object
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
responseFromLoginSignup | object |
route | string |
Example
confirm_signup_login(object, "/dashboard")
verify_local_storage() ⇒ number
goes to local storage and parses user. if user and user.token, then will return parseInt(user.id)
Kind: global function
Returns: number - user id
Date: 2021-12-30
confirm_logout(routeToGoAfter)
- removes from local storage user replaces window.location with login
Kind: global function
Date: 2022-03-16
Author: zen-out
Param | Type |
---|---|
routeToGoAfter | string |
Example
confirm_logout("/login")