npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bdt105google

v1.0.10

Published

Various google apis

Downloads

7

Readme

Connexion

This package gives functions to authentification capabilities based on JWT and MySql

Dependencies

  • bcryptjs
  • bdt105toolbox
  • jsonwebtoken
  • mysql

How does it work?

The principle is to check once against a database table with login and password. If success then a JWT is created and can be check any time without having to request the database anymore.

How to install?

npm install --save bdt105connexion

Create a table containing at least two fields: login and password (the name of the fields can be customized)

Example

CREATE TABLE `database`.`user` (
  `iduser` INT NOT NULL,
  `login` VARCHAR(200) NOT NULL,
  `password` VARCHAR(200) NOT NULL,
  PRIMARY KEY (`iduser`));

How to use?

Create an object Connexion

import { Connexion, MySqlConfiguration, JwtConfiguration } from './connexion';

let mySqlConfiguration = new MySqlConfiguration(
    "localhost", 3306, 
    "root", "root", "connexion", 
    "user", "login", "password"); // Optional - defines host, port, user, password and database to connect to MySql
let jwtConfiguration = new JwtConfiguration("secretbdt105", "$2a$10$abcdefabcdefabcdefabcd", 
    "[email protected]", "adminToken"); // Optional - defines JWT parameters

var c = new Connexion(mySqlConfiguration, jwtConfiguration);

// Add a new user
var callbackCreateUser = (err: any, data: any) =>{
    console.log("User creation result", data);
    console.log("User creation error", err);
}

var encrypt = c.encrypt("adm");

c.querySql((err: any, data: any) => callbackCreateUser(err, data), "insert into user (login, password, application) values ('admin', '" + encrypt +"', 'test')");

// get token from a user
var callbackGetUserToken = (err: any, data: any) =>{
    console.log("User check result", data);
    console.log("User check error", err);
    var newUserId = data.insertId;

    // Checks if user is valid
    var token = data;
    var decoded = c.checkJwt(token);
    console.log(decoded);
}

c.getJwt((err: any, data: any) => callbackGetUserToken(err, data), "admin", "adm");

MySqlConfiguration set all parameters to access the database and perfom the login/password verification.

class MySqlConfiguration {
    public host: string;
    public user: string;
    public password: string;
    public port: number;
    public database: string;

    public userTableName: string; // Table where users are stored
    public loginFieldName: string; // Name of the login field
    public passwordFieldName: string; // Name of the password field
}

JwtConfiguration set all parameters for JWT manipulations.

class JwtConfiguration {
    public secret: string;
    public salt: string; // Your hash "salt" parameter is used to encrypt passwords, it must start with '$2a$10$' (salt version compatible), total salt length must be >= 29.
    public userRequestEmail: string;
    public adminToken: string; // Define "adminToken" to have a default token for singup actions (adding a user without authentification).
}

If "jwtConfiguration" parameter is not set then no JWT functions are usable. If "mySqlConfiguration" parameter is not set then no MySql functions are usable. If user table name, login field name or password field name are ommited, then no authentification is possible (MySql queries remain possible). To generate an encrypted password you may use function encrypt().

Functions

querySql(callback: Function, sql: string)

Executes a sql query and calls callback function. Callback function will be like callback(err: any, rows: any).

import { Connexion } from "bdt105connexion/dist";

let configuration = new Configuration("user", "login", "password"); // defines table name, login field name, password field name
let mySqlConfiguration = new MySqlConfiguration("localhost", 3306, "admin", "admin_password", "password"); // defines host, port, user, password and database to connect to MySql
let jwtConfiguration = new JwtConfiguration("secret", "salt", "[email protected]", "adminToken"); // defines JWT parameters

let conn = new Connexion(mySqlConfiguration, jwtConfiguration, configuration);

let callback = function (err: any, rows: any){
    if (err){
        console.log("Oups error");
    }else{
        console.log("Everything is fine");
    }
}

conn.querySql(callback, "select * from order, customers where order.customerId = customer.customerId");

getJwt(callback: Function, login: string, plainPassword: string, where: string = null)

Gets Json Web Token according to login and password. Add a specific where to the query if needed. Callback function will be like callback(err: any, jwt: any).

checkJwt(token: string): Token

Returns a Token object {"token":token, "status": "OK|ERR", "decoded": decoded}. The token is valid if status="OK" and/or decode is not null.

isTokenValid(token: string) : boolean

Is the token valid?

encrypt(plain: string)

Encrypts a string according to "salt" parameter.

compareEncrypt(encrypted: string, plain: string): boolean

Compares two encrypted strings. If true then strings are equal