1.2.0 • Published 2 years ago

know_youre_real v1.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

know_youre_real

License: MIT NPM Downloads here

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)
  1. 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());
  1. Run migrations

  2. 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})
  }
})
  1. 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})
  }
})
  1. 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
  }
})
  1. 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")
}
})
  1. Pull from local storage
// will return id 
let id = kyr.verify_local_storage()
axios.post(`/api/user/${id}/task`, object).then((response) => {

}) 
  1. 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

ParamType
passwordany

hashToPassword(password, hash) ⇒ boolean

Kind: global function
Date: 2022-03-11
Author: zen-out

ParamType
passwordstring
hashstring

postDevice(knex, object) ⇒ any

  1. Get device
  2. If device exists
  3. Update the last login date
  4. Else, create new device
  5. Return device

Kind: global function
Date: 2022-03-16
Author: zen-out

ParamType
knexany
objectany

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

  1. Will grab user from user table
  2. If user exists, will return error object
  3. Otherwise, will change logged_in to true
  4. Will set last_login to today's date.

Kind: global function
Date: 2022-03-16
Author: zen-out

ParamType
knexany
userObjectany

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

ParamType
knexany
userObjectany

Example

// let firstLogin = await login(sampleRequest, knex, sampleObject)

verifyUserRoute(req, res, next) ⇒ any

  1. Checks if user is logged in

Kind: global function
Date: 2022-03-16
Author: zen-out

ParamType
reqany
resany
nextany

Example

// let verify = await verifyUserRoute(sampleRequest, knex, 1)

logout(user_id) ⇒ any

  1. Checks if user is logged in
  2. If they are, change logged in to false

Kind: global function
Date: 2022-03-16
Author: zen-out

ParamType
knexdatabase
user_idany

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

ParamType
responseFromLoginSignupobject
routestring

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

ParamType
routeToGoAfterstring

Example

confirm_logout("/login")
1.2.0

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.0.0

2 years ago