Giter Site home page Giter Site logo

wt-auth-token's Introduction

Webtask token authentication

Small repo to show how it is swift and easy to deploy a JSON web token authentication with the Webtask.io FaaS

๐Ÿ’ก Free functions expire after 1 month

getToken.js

const jwt = require('jsonwebtoken');
module.exports = (context, cb) =>  cb(null, jwt.sign({ username: context.data.username }, 'YOUR_SECRET', { expiresIn: '1h' }));

wt create getToken.js

Then retrieve with fetch()

const getToken = {
    url: 'url_of_your_webtask',
    headers: {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({username: 'chisNaN'})
    }
};

fetch(getToken.url, getToken.headers).then(r => r.json())
.then(token => console.log(token)).catch(e => console.warn(e));

checkToken.js

const jwt = require('jsonwebtoken');

module.exports = (context, cb) => {
  const token = context.headers.authorization;
  jwt.verify(token, 'YOUR_SECRET', (err, decoded) => {
    if(err) {
      cb(null, err);
    }
    if(decoded && decoded.username === 'chisNaN') {
      cb(null, `User found => ${decoded.username}`);
    }else {
      cb(null, 'User not found');
    }
  });
}

wt create checkToken.js

Then check with fetch()

const checkToken = {
    url: 'url_of_your_webtask',
    headers: {
        method: 'GET',
        headers: {
            'Authorization': 'YOUR_TOKEN'
        }
    }
};

fetch(checkToken.url, checkToken.headers).then(r => r.json())
.then(loginStatus => console.log(loginStatus)).catch(e => console.warn(e));

wt-auth-token's People

Contributors

chisnan avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.