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

[Feat] Simple Web cookie #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[Feat] Simple Web cookie #17

wants to merge 2 commits into from

Conversation

ynunezaguirre
Copy link
Contributor

Eliminates the need for redis storage, the socket service was not being used, and enables a cookie for user authorization

Eliminates the need for redis storage and the socket service, and enables a cookie at login for user authorization
@GianniCarlo GianniCarlo requested a review from harrisi March 10, 2024 03:09
Copy link
Contributor

@harrisi harrisi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path change and cookies truthiness


const loggedUser = (req: IResponse, _res: IRequest, next: INext) => {
const authorization = req.headers?.authorization;
const cookies = cookie.parse(req.headers.cookie || '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookies.parse('') returns an empty object ({}).


const loggedUser = (req: IResponse, _res: IRequest, next: INext) => {
const authorization = req.headers?.authorization;
const cookies = cookie.parse(req.headers.cookie || '');
const cookieToken = cookies ? cookies[process.env.SESSION_COOKIE_NAME] : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the note about cookies.parse(''), {} is truthy, so this will always attempt to access cookies[process.env.SESSION_COOKIE_NAME]. I think this can cause an error when the cookie is falsy for any reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a change here, right now. Just to prevent the throw error from cookie.parse. if the header doesnt has a cookie the cookies object will be an empty object {}. and cookies[process.env.SESSION_COOKIE_NAME] will be undefined and that's ok.

return res.json({ email: user.email, token });
}

public async Logout(req: IRequest, res: IResponse): Promise<IResponse> {
await res.clearCookie(process.env.SESSION_COOKIE_NAME, { path: '*' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the path here needs to match the cookie, which is '/'.

const isProd = process.env.NODE_ENV === 'production';
res.setHeader(
'Set-Cookie',
cookie.serialize(process.env.SESSION_COOKIE_NAME, token, {
Copy link
Contributor

@harrisi harrisi Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why this is provided by the environment? It's not a secret, so there's no reason this can't just be 'sessionCookie' (or whatever), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah you are right., i changed it to a constant value now.

sameSite: isProd ? 'none' : null,
secure: isProd,
path: '/',
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to set domain: tortugapower.com. As it is now (i.e., not set), the cookie is only available on https://tortugapower.com, and not its subdomains.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that the idea is that there could be different web apps from different domains that can use the api, so first validate that the domain must exist in our database . That reason is now set to none in production so the cookie will be cross domain. I think there are no problems with subdomains right now as long as the domain exists in our database, but if we are only going to work with the bookplayer domain we can make the change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Domain part of the cookie is for what the server sends and receives. The frontend can be wherever, but this is just what the client will use to decide when to send cookies with requests. The way subdomains works with cookies is kind of strange: if a server foo.example.com sends Set-Cookie to a client, the client will only send that cookie to example.com, not to foo.example.com. If the Domain is set to example.com, then the client will send the cookie to foo.example.com (and bar.example.com, etc.).

@harrisi
Copy link
Contributor

harrisi commented Mar 11, 2024

We'll want to add a refresh mechanism at some point, but I think we need to look at the authentication/authorization situation in general soon anyway.

@harrisi
Copy link
Contributor

harrisi commented Mar 11, 2024

The more I think about this, the more I think we should rethink how we're doing the auth for the web app. It probably makes more sense to stick to the API server just using JWTs and moving the currently-static web app to it's own thing. That way the web app can handle cookies and just pass JWTs to and from the API server as needed.

Although the implementation is pretty simple now, it does start to feel like it's not in the right place. I still think we can go through with this for now, though, and figure out how we want to handle separating those later, if at all.

Copy link
Contributor

@harrisi harrisi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants