1.0.1 • Published 3 years ago

easysessions v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Install

npm i easysessions

Creating a session

const sessions = require('easysessions');

console.log(sessions.create(500)) // You can change the timeout time.
// This will return a string of numbers (the session id). 

Checking if a session exists

const sessions = require('easysessions');

console.log(sessions.check(sessionID))
// Will return true or false

Creating a session that never expires

const sessions = require('easysessions');

console.log(sessions.create1Time())
// This will return a string of numbers (the session id). 

Once you want to delete this session you can use

const sessions = require('easysessions');

sessions.delete(id)

Examples Of Use:

const express = require('express');
const sessions = require('easysessions');
const app = express();

app.get('/', (req, res) => {
    res.render('index.ejs', {
        session: sessions.create1Time()
    })// The page loads with a infinate session
})

app.post('/api/v1/someapi/:session', function(req, res){
    if(sessions.check(req.params.session)){
        sessions.delete(req.params.session)

        res.json({ ok: true })
    } else{
        res.json({ error: "Invaild Session" })
    }
})// Have an api callback with a session in the url

app.listen(80)

Client Side:

<button onclick="submit()">Test</button>

<script>
    function submit(){
        fetch('./api/v1/someapi/<%= session %>', {
            method: "POST",
        }).then(data => data.json()).then(data => {
            if(data.error)return console.log('Session was invaild')

            if(data.ok){
                console.log('Session was correct')
            }
        })
    }
</script>
1.0.1

3 years ago

1.0.0

3 years ago