Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Conversation

@Isaac95Cr
Copy link
Collaborator

Add user login

@Isaac95Cr Isaac95Cr requested a review from ajdurancr December 14, 2021 21:48
@@ -0,0 +1,59 @@
import React, { useState, useEffect } from 'react'
import {redirectTo, getCookieValue, setCookie} from '../utils'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please consider using an existing library to manage cookies. For example js-cookie which allows you to .set, .get, .remove and other methods that also allows you to set other attributes.

import {redirectTo, getCookieValue, setCookie} from '../utils'
import * as styles from '../index.module.sass'

const HOME = 'http://localhost:8000'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use environment variables with self-descriptive names. For example: URL_HOME and URL_OAUTH_LOGIN.

See more here.

const HOME = 'http://localhost:8000'
const SERVER_OUTH_LOGIN = 'http://localhost:3000/oauth/login'

const resetUserCookies = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move this function to the utils.js file and pass the cookies to delete as an argument. For example:

const deleteCookies = (cookiesToDelete: string[]): void => { ... }
})
}

interface user {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use names in PascaCase format for TypeScript interfaces, type, enums, etc. For example: User

}

const Login = () => {
const [user, setUser] = useState<user | undefined>(undefined)
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no need to pass undefined as argument since it's the default value for a the useState hook.


useEffect(() => {
const userCookie = getCookieValue('user')
if(userCookie) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can use a breaker instead:

... if (!userCookie) return ...
}
} , [])

const onLogoutClick = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use the useCallback hook to wrap this function.

redirectTo(HOME)
}

const onLoginClick = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here about using useCallback.

)
}

export default Login No newline at end of file
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would strongly recommend using a named export instead of the default one.

@@ -0,0 +1,14 @@
export const redirectTo = (url: string) => (window.location.href = url)

export const getCookieValue = (name: string): string|undefined => (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove this cookie helpers after using a js library to manage cookies.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

2 participants