Skip to content

Commit

Permalink
add isAnonymous to fix auth redirect of public users
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 22, 2024
1 parent fa79417 commit 8fd0bed
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
16 changes: 11 additions & 5 deletions lib/Auth/AuthTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ export type SessionUser<
* id and type values will be available in the prostgles.user session variable in postgres
* */
user: ServerUser;

/**
* If true, this is a public/non registered user that can login. Used in UI
*/
isAnonymous?: boolean;

/**
* Controls which fields from user are available in postgres session variable
*/
Expand All @@ -240,14 +246,14 @@ export type SessionUser<
clientUser: ClientUser;
};

type AllNeverAndOptional<T> = {
[P in keyof T]?: never;
};
export type AuthResultWithSID<SU = SessionUser> =
| (SU & { sid: string })
| {
| (AllNeverAndOptional<SU> & {
sid: string | undefined;
user?: never;
sessionFields?: never;
clientUser?: never;
};
});

export type AuthResult<SU = SessionUser> = SU | undefined;
export type AuthResultOrError<SU = SessionUser> = AuthFailure["code"] | AuthResult<SU>;
Expand Down
12 changes: 6 additions & 6 deletions lib/Auth/endpoints/setCatchAllRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ export function setCatchAllRequestHandler(this: AuthHandler, app: e.Express) {
return;

/** If Logged in and requesting login then redirect to main page */
} else if (
this.matchesRoute(AUTH_ROUTES_AND_PARAMS.login, req.path) &&
(await isLoggedInUser())
) {
res.redirect("/");
return;
} else if (this.matchesRoute(AUTH_ROUTES_AND_PARAMS.login, req.path)) {
const { user, isAnonymous } = await getUser();
if (user && !isAnonymous) {
res.redirect("/");
return;
}
}

onGetRequestOK?.(req, res, {
Expand Down
4 changes: 4 additions & 0 deletions lib/Auth/endpoints/setConfirmEmailRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export function setConfirmEmailRequestHandler(
.status(HTTP_FAIL_CODES.BAD_REQUEST)
.json({ success: false, code: "something-went-wrong" });
}

/**
* This approach requires correct handling in setCatchAllRequestHandler to not redirect user.type=public res.redirect("/");
*/
if (response.redirect_to) {
return res.redirect(response.redirect_to);
}
Expand Down
8 changes: 3 additions & 5 deletions lib/Auth/utils/getSidAndUserFromRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ export async function getSidAndUserFromRequest(
*/
const getSession = this.opts.cacheSession?.getSession;
if (clientReq.socket && getSession && clientReq.socket.__prglCache) {
const { session, user, clientUser } = clientReq.socket.__prglCache;
const { session, ...userData } = clientReq.socket.__prglCache;
const isValid = this.isNonExpiredSocketSession(clientReq.socket, session);
if (isValid) {
return {
...userData,
sid: session.sid,
user,
clientUser,
};
} else
return {
Expand All @@ -47,9 +46,8 @@ export async function getSidAndUserFromRequest(
const session = await getSession(sid, this.dbo as any, this.db);
if (session && session.expires && clientInfo?.user) {
clientReq.socket.__prglCache = {
...clientInfo,
session,
user: clientInfo.user,
clientUser: clientInfo.clientUser,
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/DboBuilder/DboBuilderTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
TableInfo as TInfo,
UserLike,
} from "prostgles-types";
import { AuthClientRequest, BasicSession } from "../Auth/AuthTypes";
import { AuthClientRequest, BasicSession, SessionUser } from "../Auth/AuthTypes";
import { BasicCallback } from "../PubSubManager/PubSubManager";
import { PublishAllOrNothing } from "../PublishParser/PublishParser";
import { FieldSpec } from "./QueryBuilder/Functions";
Expand Down Expand Up @@ -170,9 +170,9 @@ export type PRGLIOSocket = {
/** Used for session caching */
__prglCache?: {
session: BasicSession;
user: UserLike;
clientUser: UserLike;
};
// user: UserLike;
// clientUser: UserLike;
} & SessionUser;

_user?: AnyObject;

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.199",
"version": "4.2.200",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8fd0bed

Please sign in to comment.