Gathering detailed insights and metrics for maranda-koa2-session-mysql
Gathering detailed insights and metrics for maranda-koa2-session-mysql
Gathering detailed insights and metrics for maranda-koa2-session-mysql
Gathering detailed insights and metrics for maranda-koa2-session-mysql
npm install maranda-koa2-session-mysql
Typescript
Module System
Node Version
NPM Version
JavaScript (52.42%)
TypeScript (47.58%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
4,466
Last Day
7
Last Week
8
Last Month
90
Last Year
587
1 Stars
35 Commits
5 Branches
1 Contributors
Updated on Jun 25, 2019
Latest Version
3.1.9
Package Id
maranda-koa2-session-mysql@3.1.9
Unpacked Size
20.21 kB
Size
6.30 kB
File Count
7
NPM Version
6.9.0
Node Version
12.4.0
Cumulative downloads
Total Downloads
Last Day
0%
7
Compared to previous day
Last Week
-27.3%
8
Compared to previous week
Last Month
2,900%
90
Compared to previous month
Last Year
121.5%
587
Compared to previous year
4
app.ts
1import Session, { Sequelize } from 'maranda-koa2-session-mysql' 2 3import Koa from 'koa'; 4 5interface SessionData extends Session.DataType{ 6 name:string, 7 id: number, 8 stauts:boolean, 9 friend: sessionData, 10 group: [number,string,sessionData] 11} 12export interface Ctx extends Session.Ctx<SessionData> { 13 //if have other context 14} 15const app = new Koa<any, Ctx>(); 16const sequelize = new Sequelize('xxx', 'xxx', 'xxx', { 17 dialect: 'mysql', 18 host: 'localhost', 19 port: 3306, 20}) 21//you can set the gc probability whith molecula and denominato(this example 5/1000, default 1/100), tableName(custom tablename, default sessions), gcType('auto' or 'manul', if you set it to 'manul, you may do the session gc work by your self), ... 22//you mast ensure that there is not table named 'sessions' or your custom tablename in your database_schema 23app.use(Session.middware( 24 sequelize, 25 { 26 gcOpts: { probDenominator: 100, probMolecular: 1, type: 'auto' }, 27 defaultExpiry: 24 * 60 * 60 * 1000 //if the client close without set the session expiry, the some client will not create session again in 1 day, 28 } 29)); 30app.use((ctx, next) => { 31 if (ctx.path == '/'){ 32 if (ctx.session.isNewRecord) { // if true means that there is no session or the session has been expired of the request 33 ctx.body = `please login` 34 }else{ 35 ... 36 } 37 } 38}); 39app.use((ctx, next) => { 40 if(ctx.path == '/login'){ 41 const userCode = 'ss', 42 passWord = 'sss', 43 expiry = 5*24*60*60*1000; //5 days 44 .... 45 ctx.session.data.friend.id = 'xxx'; 46 ctx.session.data.id = code; 47 ctx.session.expiryTo = new Date(Date.now()+expiry); 48 //or you can set expiry as : 49 ctx.session.expiry = expiry; //means ctx.session.expiryTo = new Date(ctx.session.createAt.getTime() + expiry) 50 // if you do not set expiry or the expiryTo, we set cookie by defult, means when you close the window, the session cookie will be deleted 51 // if you do save session data manully, like 'ctx.session.save()', you must set the cookies by your self, like 'ctx.cookie.set(...)' 52 await next(); 53 //do not set your session data after next, because it will never work only if you do save session data munully, like 'ctx.session.save()', and then set the cookies by your self 54 .... 55 } 56}); 57app.use((ctx, next) => { 58 if (ctx.path == '/logout' && !ctx.session.isNewRecord) { 59 await ctx.session.destroy(); 60 } 61}); 62 63app.listen(80);
No vulnerabilities found.