Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export session to @lunoxjs/session #45

Merged
merged 10 commits into from
Jun 12, 2023
Merged
13 changes: 8 additions & 5 deletions packages/lunox-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/auth",
"version": "2.0.0-next.7",
"version": "2.0.0-next.8",
"description": "Lunox Authentication",
"main": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -29,14 +29,17 @@
},
"license": "MIT",
"devDependencies": {
"@lunoxjs/build": "workspace:2.0.0-next.7",
"@lunoxjs/core": "workspace:2.0.0-next.7",
"@lunoxjs/build": "workspace:2.0.0-next.8",
"@lunoxjs/core": "workspace:2.0.0-next.8",
"rollup": "^3.19.1",
"typescript": "^4.9.5"
},
"peerDependencies": {
"@lunoxjs/core": "workspace:2.0.0-next.7"
"@lunoxjs/core": "workspace:2.0.0-next.8",
"@lunoxjs/session": "workspace:2.0.0-next.8"
},
"type": "module",
"dependencies": {}
"dependencies": {
"@lunoxjs/session": "workspace:2.0.0-next.8"
}
}
18 changes: 2 additions & 16 deletions packages/lunox-auth/src/AuthManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application, useMagic } from "@lunoxjs/core";
import type { Class, Request } from "@lunoxjs/core/contracts";
import { Application, useMagic, Request } from "@lunoxjs/core";
import type { Class } from "@lunoxjs/core/contracts";
import type { GuardConfig, UserProviderConfig } from "./contracts/Config";
import type { Guard, StatefulGuard, UserProvider } from "./contracts";

Expand Down Expand Up @@ -59,20 +59,6 @@ export class AuthManager {
return (this.constructor as typeof AuthManager).createDriver(name, config);
}

// protected createSessionDriver(name: string, config: Record<string, any>) {
// const provider = this.createUserProvider(config["provider"]);
// const guard = new SessionGuard(
// name,
// provider,
// this.request.session(),
// this.request
// );
// if (config.remember) {
// guard.setRememberDuration(config.remember);
// }
// return guard;
// }

public static createDriver(name: string, config: GuardConfig) {
const driverCreator = this.drivers[config.driver];
if (!driverCreator) {
Expand Down
26 changes: 11 additions & 15 deletions packages/lunox-auth/src/AuthServiceProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ServiceProvider, Request } from "@lunoxjs/core";
import type { Request as RequestContract } from "@lunoxjs/core/contracts";
import AuthManager, {
AuthManager as AuthManagerContracts,
} from "./AuthManager";
Expand All @@ -9,31 +8,28 @@ import SessionGuard from "./SessionGuard";
class AuthServiceProvider extends ServiceProvider {
async register(): Promise<void> {
this.app.singleton(AuthManager.symbol, () => new AuthManager(this.app));
}
async boot(): Promise<void> {
AuthManager.registerDriver("session", function (name, config) {
const provider = AuthManager.createUserProvider(config["provider"]);
const guard = new SessionGuard(name, provider, request());
if (config.remember) {
guard.setRememberDuration(config.remember);
}
return guard;
});

// create macro to support request.auth() method
Request.macro("auth", function (this: RequestContract) {
Request.macro("auth", function(this: Request) {
if (this.managers["auth"]) {
return this.managers["auth"] as AuthManagerContracts & StatefulGuard;
}
return (this.managers["auth"] = new AuthManager(this.app).setRequest(
this
));
});
AuthManager.registerDriver("session", function(name, config) {
const provider = AuthManager.createUserProvider(config["provider"]);
const guard = new SessionGuard(name, provider, request());
if (config.remember) {
guard.setRememberDuration(config.remember);
}
return guard;
});
}
async boot(): Promise<void> { }
}

// augmentation Request interface for better typechecking
declare module "@lunoxjs/core/contracts" {
declare module "@lunoxjs/core" {
interface Request {
auth(): AuthManagerContracts & StatefulGuard;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/lunox-auth/src/SessionGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
} from "./contracts";
import GuardHelper from "./GuardHelpers";
import Recaller from "./Recaller";
import type { Request } from "@lunoxjs/core/contracts";
import { SessionManager, Str } from "@lunoxjs/core";
import { Str, Request } from "@lunoxjs/core";
import type { SessionManager } from "@lunoxjs/session";

class SessionGuard extends GuardHelper implements StatefulGuard {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-build/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/build",
"version": "2.0.0-next.7",
"version": "2.0.0-next.8",
"description": "Lunox helper for rollup",
"main": "index.mjs",
"files": [
Expand Down
1 change: 0 additions & 1 deletion packages/lunox-core/contracts.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./dist/Contracts";
export { RequestContract as Request, RouterContract as Router } from "./dist";
2 changes: 1 addition & 1 deletion packages/lunox-core/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type {
Application,
Env,
RequestContract,
Request as RequestContract,
RedirectResponse,
ExtendedFacade,
ResponseRenderer,
Expand Down
5 changes: 1 addition & 4 deletions packages/lunox-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lunoxjs/core",
"version": "2.0.0-next.7",
"version": "2.0.0-next.8",
"description": "Laravel-Flavoured NodeJs framework",
"bin": {
"lunox": "./bin/lunox.cjs"
Expand Down Expand Up @@ -64,7 +64,6 @@
"commander": "^9.2.0",
"cookie": "0.4.1",
"dotenv": "^10.0.0",
"express-session": "^1.17.3",
"formidable": "^2.0.1",
"hash-equals": "^0.0.5",
"pluralize": "^8.0.0",
Expand All @@ -76,7 +75,6 @@
"@types/bcryptjs": "^2.4.2",
"@types/cookie": "^0.4.1",
"@types/cors": "^2.8.12",
"@types/express-session": "^1.17.4",
"@types/formidable": "^2.0.0",
"@types/jest": "^27.4.1",
"@types/node": "^16.11.7",
Expand All @@ -99,7 +97,6 @@
"react-dom": "^18.1.0",
"react-helmet": "^6.1.0",
"rollup": "^3.19.1",
"session-file-store": "^1.5.0",
"sqlite3": "^5.0.3",
"supertest": "^6.2.4",
"svelte": "^3.46.6",
Expand Down
13 changes: 0 additions & 13 deletions packages/lunox-core/src/Contracts/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,3 @@ export interface AppConfig {
cipher: CipherTypes;
providers: (typeof ServiceProvider)[];
}

export interface SessionConfig {
driver: "file" | "cookie" | "database";
lifetime: number;
files: string;
table: string;
cookie: string;
path: string;
domain?: string;
http_only: boolean;
same_site: "lax" | "strict" | "none" | null;
secure: boolean;
}
6 changes: 0 additions & 6 deletions packages/lunox-core/src/Contracts/Request.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { Request as ServerRequest } from "polka";
import type { Session } from "express-session";
import type { Request as HttpRequest } from "../Http/Request";

export interface ExtendedRequest extends ServerRequest {
session?: Session;
}

export interface FormRequest<Validator = any> extends HttpRequest {
/**
* Get rules for validator.
Expand Down
48 changes: 28 additions & 20 deletions packages/lunox-core/src/Foundation/Exception/Handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import HttpException from "../../Http/HttpException";
import RedirectResponse from "../../Http/RedirectResponse";
import type Container from "../../Container/Container";
import type { Request } from "../../Http/Request";
import { Request } from "../../Http/Request";
import type HttpResponse from "../../Http/Response";
import Response from "../../Support/Facades/Response";
import type { Class } from "../../Types";
import { TokenMismatchException } from "../../Session";
import type { ExceptionHandler } from "../../Contracts/Exception/Handler";
import { ViewFactory } from "../../Http";

Expand All @@ -19,16 +18,24 @@ interface reportCallback<E> {
exception: Class<E>;
reportUsing: reportUsing<E>;
}
interface MapException {
type: Class<Error>;
value: (e: Error) => HttpException;
}
class Handler implements ExceptionHandler {
public static symbol = Symbol("ExceptionHandler");
protected static internalDontReport: Class<Error>[] = [HttpException];
protected static mapExceptions: MapException[] = [];
protected container: Container;
protected reportCallbacks: reportCallback<any>[] = [];
protected renderCallbacks: renderCallback<any>[] = [];
protected dontReport: Class<Error>[] = [];
protected internalDontReport: Class<Error>[] = [
HttpException,
TokenMismatchException,
];
public static addInternalDontReport(errorClass: Class<Error>) {
this.internalDontReport.push(errorClass);
}
public static addMapException(mapException: MapException) {
this.mapExceptions.push(mapException);
}

constructor(container: Container) {
this.container = container;
Expand All @@ -49,7 +56,9 @@ class Handler implements ExceptionHandler {
if (response instanceof RedirectResponse) {
response.setRequest(req);
// make sure all session is saved
await req.session().save();
if (Request.hasMacro("session")) {
await (req as any).session().save();
}
}

if (response instanceof ViewFactory) {
Expand Down Expand Up @@ -81,19 +90,15 @@ class Handler implements ExceptionHandler {
}

protected prepareException(e: any) {
const mapException = [
{
type: TokenMismatchException,
value: (e: TokenMismatchException) =>
new HttpException(419, e.message, e),
return (this.constructor as typeof Handler).mapExceptions.reduce(
(prev: HttpException, map) => {
if (e instanceof map.type) {
prev = map.value(e);
}
return prev;
},
];
return mapException.reduce((prev, map) => {
if (e instanceof map.type) {
prev = map.value(e);
}
return prev;
}, e);
e
);
}

public report(e: any) {
Expand Down Expand Up @@ -127,7 +132,10 @@ class Handler implements ExceptionHandler {
protected register() { }

protected shouldntReport(e: Class<Error>) {
const dontReport = [...this.dontReport, ...this.internalDontReport];
const dontReport = [
...this.dontReport,
...(this.constructor as typeof Handler).internalDontReport,
];
return dontReport.findIndex((x) => e instanceof x) >= 0;
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/lunox-core/src/Foundation/Http/Kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ class Kernel {

if (response instanceof HttpResponse) {
// make sure all session is saved
await httpRequest.session().save();
if (HttpRequest.hasMacro("session")) {
await (httpRequest as any).session().save();
}
httpResponse.mergeResponse(response);
httpResponse.setCookiesToHeaders();
return this.send(
Expand Down
3 changes: 1 addition & 2 deletions packages/lunox-core/src/Foundation/Http/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Cookie from "./Cookie";
import Kernel from "./Kernel";
import VerifyCsrfToken from "./Middleware/VerifyCsrfToken";
export { Kernel, VerifyCsrfToken, Cookie };
export { Kernel, Cookie };
14 changes: 10 additions & 4 deletions packages/lunox-core/src/Http/RedirectResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Request from "./Request";
import Request from "./Request";
import Response from "./Response";

class RedirectResponse extends Response {
Expand Down Expand Up @@ -31,11 +31,17 @@ class RedirectResponse extends Response {
delete inputs[key];
}
});
req.session().put("__old", inputs);
if (Request.hasMacro("session")) {
(req as any).session().put("__old", inputs);
}
}
if (Request.hasMacro("session")) {
const __session = (req as any).session().getFlashed();
(req as any)
.session()
.put("__session", { ...__session, ...this.session });
}
// merge flashed session and withInput session
const __session = req.session().getFlashed();
req.session().put("__session", { ...__session, ...this.session });
return req;
}

Expand Down
Loading