This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+55
View File
@@ -0,0 +1,55 @@
/// <reference path="../../adonis-typings/index.d.ts" />
/// <reference types="@adonisjs/http-server/build/adonis-typings" />
import { SessionConfig, SessionDriverContract, SessionClientContract } from '@ioc:Adonis/Addons/Session';
import { CookieClientContract } from '@ioc:Adonis/Core/CookieClient';
import { Store } from '../Store';
/**
* SessionClient exposes the API to set session data as a client
*/
export declare class SessionClient extends Store implements SessionClientContract {
private config;
private driver;
private cookieClient;
/**
* Each instance of client works on a single session id. Generate
* multiple client instances for a different session id
*/
private sessionId;
/**
* Session key for setting flash messages
*/
private flashMessagesKey;
/**
* Flash messages store. They are merged with the session data during
* commit
*/
flashMessages: Store;
constructor(config: SessionConfig, driver: SessionDriverContract, cookieClient: CookieClientContract, values: {
[key: string]: any;
} | null);
/**
* Find if the sessions are enabled
*/
isEnabled(): boolean;
/**
* Load session from the driver
*/
load(cookies: Record<string, any>): Promise<{
session: any;
flashMessages: any;
}>;
/**
* Commits the session data to the session store and returns
* the session id and cookie name for it to be accessible
* by the server
*/
commit(): Promise<{
sessionId: string;
signedSessionId: string;
cookieName: string;
}>;
/**
* Clear the session store
*/
forget(): Promise<void>;
}
+93
View File
@@ -0,0 +1,93 @@
"use strict";
/*
* @adonisjs/session
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionClient = void 0;
/// <reference path="../../adonis-typings/index.ts" />
const helpers_1 = require("@poppinss/utils/build/helpers");
const Store_1 = require("../Store");
/**
* SessionClient exposes the API to set session data as a client
*/
class SessionClient extends Store_1.Store {
constructor(config, driver, cookieClient, values) {
super(values);
this.config = config;
this.driver = driver;
this.cookieClient = cookieClient;
/**
* Each instance of client works on a single session id. Generate
* multiple client instances for a different session id
*/
this.sessionId = (0, helpers_1.cuid)();
/**
* Session key for setting flash messages
*/
this.flashMessagesKey = '__flash__';
/**
* Flash messages store. They are merged with the session data during
* commit
*/
this.flashMessages = new Store_1.Store({});
}
/**
* Find if the sessions are enabled
*/
isEnabled() {
return this.config.enabled;
}
/**
* Load session from the driver
*/
async load(cookies) {
const sessionIdCookie = cookies[this.config.cookieName];
const sessionId = sessionIdCookie ? sessionIdCookie.value : this.sessionId;
const contents = await this.driver.read(sessionId);
const store = new Store_1.Store(contents);
const flashMessages = store.pull(this.flashMessagesKey, null);
return {
session: store.all(),
flashMessages,
};
}
/**
* Commits the session data to the session store and returns
* the session id and cookie name for it to be accessible
* by the server
*/
async commit() {
this.set(this.flashMessagesKey, this.flashMessages.all());
await this.driver.write(this.sessionId, this.toJSON());
/**
* Clear from the session client memory
*/
this.clear();
this.flashMessages.clear();
return {
sessionId: this.sessionId,
signedSessionId: this.cookieClient.sign(this.config.cookieName, this.sessionId),
cookieName: this.config.cookieName,
};
}
/**
* Clear the session store
*/
async forget() {
/**
* Clear from the session client memory
*/
this.clear();
this.flashMessages.clear();
/**
* Clear with the driver
*/
await this.driver.destroy(this.sessionId);
}
}
exports.SessionClient = SessionClient;