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

Antti | kirjautuminen ja rekistöröityminen sekä authentication #23

Merged
merged 129 commits into from May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
7defc17
Copied pages/login.tsx file from old login branch
anttiasmala Apr 4, 2024
f028cfb
Fixed imports and deleted unnecessary stuff
anttiasmala Apr 4, 2024
3d680e1
Fixed some frontend code in login.tsx
anttiasmala Apr 4, 2024
7a987a7
Fixed name attributes in Inputs
anttiasmala Apr 4, 2024
ed507b4
Installed lucia and oslo packages
anttiasmala Apr 4, 2024
44da0e4
Installed @lucia-auth/adapter-prisma package
anttiasmala Apr 4, 2024
0e50733
Updated favicon.ico with working icon
anttiasmala Apr 4, 2024
43d3108
Added Sessions to schema.prisma
anttiasmala Apr 4, 2024
7aa985d
Changed default password for development only
anttiasmala Apr 4, 2024
59b6a17
Rewriting register.tsx. Currently added some debugging console.logs
anttiasmala Apr 4, 2024
7aadfbc
Copied an example of middleware from lucia-auth's website. Reference:…
anttiasmala Apr 4, 2024
c0cb21a
Copied an example of auth from lucia-auth's website: References: http…
anttiasmala Apr 4, 2024
9d494c1
Created a new folder pages/api/auth and added register.ts file there …
anttiasmala Apr 4, 2024
4f580c3
Added checks if values given in registering are valid or not
anttiasmala Apr 4, 2024
e7f4640
Created a file for URLs to images that have been taken from internet …
anttiasmala Apr 4, 2024
de68abf
Created a custom header check so backend can call backend APIs withou…
anttiasmala Apr 4, 2024
0787a77
Created a simple request for registering. Added default values for fi…
anttiasmala Apr 4, 2024
e61837f
Commented isEmailValid function out for now
anttiasmala Apr 4, 2024
6f33e73
Made the CUSTOM_HEADER variable exportable so it does not need to be …
anttiasmala Apr 4, 2024
a73c274
Removed old createUser function and made userCreatedSuccesfully funct…
anttiasmala Apr 4, 2024
5eed346
Added a simple text that will tell if it is in production or not
anttiasmala Apr 4, 2024
21d9f98
Added functions for checking that given values are valid. Added reque…
anttiasmala Apr 4, 2024
6913f2e
ESLint fixes
anttiasmala Apr 4, 2024
2b9c5fd
Updated .env.example file
anttiasmala Apr 4, 2024
3a4ac71
Removed paragraph that tells if is in production or development stage
anttiasmala Apr 5, 2024
6271496
Created a type when creating session into database
anttiasmala Apr 5, 2024
15f0d57
Changed sessionId to id, added userId, createdAt and updatedAt. Made …
anttiasmala Apr 5, 2024
050eb73
Removed globalThis.crypto variable. Added DatabaseSessionAttributes t…
anttiasmala Apr 5, 2024
e9f014f
Added a function to create a session when user registers and made it …
anttiasmala Apr 5, 2024
882ec6f
Created a new file utils.ts to backend to have all isEmailValid etc f…
anttiasmala Apr 5, 2024
add75ca
Created login file into /api/auth. Added some basic code into it
anttiasmala Apr 5, 2024
c871d5b
Moved isPasswordValid etc into backend/utils
anttiasmala Apr 5, 2024
143e7ba
Made a new type for user when logging in
anttiasmala Apr 5, 2024
5a8a25e
Made functions in backend/utils.ts to return false instead of throwin…
anttiasmala Apr 5, 2024
76e6afb
Changed the way how errors were handled
anttiasmala Apr 5, 2024
2fe00c4
Created a function base for session handler
anttiasmala Apr 7, 2024
e742dfa
Changed default email and password values for development only
anttiasmala Apr 7, 2024
b12f3e9
Due to modification to isEmailValid etc functions had to change them …
anttiasmala Apr 7, 2024
94ab476
Took exact values from req.body to make code a lot easier to read
anttiasmala Apr 8, 2024
6c1ea14
Removed errorFound function. Replaced it with backend's handleError f…
anttiasmala Apr 8, 2024
61bf1d7
Moved hashPassword from /api/users/index.ts to -> backend/utils.ts to…
anttiasmala Apr 8, 2024
b2d2950
Removed isEmailValid function due to check is done in backend. Remove…
anttiasmala Apr 8, 2024
39b279a
Created a new function for creating user
anttiasmala Apr 8, 2024
b2af52d
Added a check if given userDetails are valid
anttiasmala Apr 8, 2024
430ceb8
ESLint fixes
anttiasmala Apr 8, 2024
ddef858
Changed the way how email and password variables are declared
anttiasmala Apr 8, 2024
0195118
Removed couple values in .env.example. Not used right now. In /api/au…
anttiasmala Apr 8, 2024
f26dded
Removed an import that was not used in /api/auth/register.ts
anttiasmala Apr 8, 2024
5fae57c
Merge branch 'main' into antti/register
anttiasmala Apr 8, 2024
594dbf3
Removed CUSTOM_HEADER in middleware.ts
anttiasmala Apr 8, 2024
356244a
Made createUser function in backend exportable. Fixed the import in /…
anttiasmala Apr 8, 2024
b7d3469
Made isEmailValid function to return false instead throwing an error
anttiasmala Apr 8, 2024
3d58c21
Merged main branch into antti/register
anttiasmala Apr 8, 2024
765d283
Fixed regexes imports and removed HttpError due to no usage
anttiasmala Apr 8, 2024
2ab6d2a
Added a simple checker that checks if the error is known
anttiasmala Apr 8, 2024
e19df78
handleError file's handleUserError now returns a string. Added a new …
anttiasmala Apr 8, 2024
bf30cfd
Added eye_open.svg and eye_slash.svg. Converted them to .tsx files by…
anttiasmala Apr 8, 2024
7a7000d
Added eye icon behind password which will show the password. Added fu…
anttiasmala Apr 10, 2024
f6613eb
Added firstName and lastName that should not be taken when user tries…
anttiasmala Apr 10, 2024
755d369
Changed UserLoginDetails type
anttiasmala Apr 10, 2024
d68195e
Merged main branch into antti/login
anttiasmala Apr 10, 2024
734fa10
Created a prototype of cookie validation
anttiasmala Apr 10, 2024
a91e5ff
Fixed a few errors. Fixed session creation
anttiasmala Apr 10, 2024
cb7b3e4
Merged antti/register into antti/login
anttiasmala Apr 10, 2024
91d633c
Changed Login function name to Register
anttiasmala Apr 10, 2024
50a4100
Added a check to see if user is logged in. Removed return from the ro…
anttiasmala Apr 11, 2024
7b051aa
Changed LuciaUser to User and added session to express in two days fo…
anttiasmala Apr 11, 2024
c8ef5d4
Added a session checker into index.tsx
anttiasmala Apr 11, 2024
8cbdff2
Created a logout function
anttiasmala Apr 11, 2024
2500743
Created a new SVG user and converted it to .tsx file"
anttiasmala Apr 11, 2024
8cd8c4f
Added credits for the images
anttiasmala Apr 11, 2024
fad4a56
Added credits for the SVG image and converted a .tsx file of it
anttiasmala Apr 12, 2024
d5445dc
Added a prototype of small info box when clicking user icon
anttiasmala Apr 12, 2024
5ddda6a
Added a prototype of logout button
anttiasmala Apr 14, 2024
5467447
Installed the needed packages on my laptop
anttiasmala Apr 14, 2024
be218f1
Added error related useStates
anttiasmala Apr 14, 2024
8561fc7
Renamed handleUserError to handleRegisterError
anttiasmala Apr 14, 2024
8075932
Created a new file isValidFunctions.ts to shared folder to be able to…
anttiasmala Apr 14, 2024
1eb0088
Created useState to determite if the user details should be shown. Ad…
anttiasmala Apr 14, 2024
c9d21be
Moved error text block a bit more up so it will not resize the whole …
anttiasmala Apr 14, 2024
70901ba
Created a function for login errors and added login error to known fr…
anttiasmala Apr 14, 2024
74a2953
Added an eye that will reval the password. Copied it from register.tsx
anttiasmala Apr 14, 2024
908f6d4
Made adapter variable exportable
anttiasmala Apr 14, 2024
1530a3a
Changed UserLoginDetails type
anttiasmala Apr 14, 2024
7de5002
Created a useState for rememberMe checkbox and added functionality fo…
anttiasmala Apr 14, 2024
b3e9573
Created a new function luciaLongSession and changed default lucia to …
anttiasmala Apr 14, 2024
f9f28c7
Changed 'regular' session to be 2 weeks. Remember me wil be 30 days
anttiasmala Apr 15, 2024
23a46da
Added frontend styling
anttiasmala Apr 15, 2024
b285acf
Created a local component for user detail modal. Might change it in t…
anttiasmala Apr 15, 2024
4c9563d
Created a function called handleLogout and added it to UserDetailsMod…
anttiasmala Apr 15, 2024
ae6f4b9
Removed the extra sign out button
anttiasmala Apr 15, 2024
c0876b1
Added hoverOnlyWhenSupported to be true to make hover elements work a…
anttiasmala Apr 15, 2024
412c37f
Added some functionality to classname rendering. Created a new parame…
anttiasmala Apr 15, 2024
99b8d7d
Removed development placeholder values
anttiasmala Apr 15, 2024
513cb3a
Removed development placeholder values
anttiasmala Apr 16, 2024
5bce277
ESLint fixes
anttiasmala Apr 16, 2024
60688a6
Added secure value to /api/login.ts's Lucia class constructor
anttiasmala Apr 16, 2024
0c44e77
Created a migration of Session Model
anttiasmala Apr 20, 2024
abcb4a6
Removed unnecessary lines in .env.example
anttiasmala Apr 22, 2024
c1678cf
Moved handleLogout function from Home function to UserDetailModal fun…
anttiasmala Apr 29, 2024
00b11da
Changed the way how frontend error texts are set
anttiasmala Apr 29, 2024
300226e
Combined login and register errors and fixed the imports
anttiasmala Apr 29, 2024
994ca51
Moved luciaLongSession to .auth file
anttiasmala Apr 29, 2024
5d5d95e
Changed shortLuciaSession to be 1 hour instead of 14 days
anttiasmala Apr 29, 2024
a1c770b
Created a new file getServerSideProps
anttiasmala May 1, 2024
a7861a1
Changes to backend/auth.ts file
anttiasmala May 1, 2024
1d87410
Debugging
anttiasmala May 1, 2024
c74d47a
Added export types
anttiasmala May 6, 2024
0766e18
Imported User as LuciaUser from lucia module
anttiasmala May 6, 2024
d5a4543
Removed as statement in pages/register.tsx
anttiasmala May 6, 2024
7d85f3f
Added a new variable SvgEye to determine which SVG icon will be shown…
anttiasmala May 6, 2024
58133a1
Edited getServerSideProps in login.tsx to not get the return an objec…
anttiasmala May 10, 2024
d8e4758
Removed commented getServerSideProps function
anttiasmala May 10, 2024
2bf0766
Made code easier to read
anttiasmala May 10, 2024
318e45c
Removed customDestination parameter. Removed an as statement
anttiasmala May 10, 2024
1c4c157
ESLint fixes
anttiasmala May 10, 2024
22ac78b
Rewrote isValidFunctions.ts isEmailValid function
anttiasmala May 10, 2024
508d84a
Added a check to only return the gifts the requester has an access to…
anttiasmala May 13, 2024
9c973ac
Added a check to only return the gifts, allow updating the gift and d…
anttiasmala May 13, 2024
efc9171
Removed debugging console.logs
anttiasmala May 13, 2024
1f6afac
Re-positioned Gift, User, Session export
anttiasmala May 15, 2024
db064d0
Reverting the removal of userId in Session model inside schema.prisma…
anttiasmala May 15, 2024
ef0882f
Added DatabaseSessionAttributes to have string value called userUUID
anttiasmala May 15, 2024
724178a
Changed variable userCreationRequest to be userData. Replaced the con…
anttiasmala May 15, 2024
7e9373b
Replaced AND statments with a better solution
anttiasmala May 15, 2024
135dfe5
Replaced connects with a better solution
anttiasmala May 15, 2024
7e4a63a
Fixed handleDELETE to not use AND statement in pages/api/gifts/[uuid]…
anttiasmala May 16, 2024
bb966a5
Fixed import/newline-after-import error
anttiasmala May 16, 2024
57a8e30
Fixed handleGET to not use AND statement in /pages/api/gifts/[uuid].ts
anttiasmala May 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .env.example
@@ -1 +1,5 @@
DATABASE_URL="postgresql://databaseusername:databasepassword@localhost:5432/mydb?schema=public"
DATABASE_URL="postgresql://databaseusername:databasepassword@localhost:5432/mydb?schema=public" # README.md's section "Setting the environment variables" will help with this
POSTGRES_USERNAME="databaseusername" # Postgres database's username. It is same as DATABASE_URL's databaseusername
POSTGRES_PASSWORD="databasepassword" # Postgres database's password. It is same as DATABASE_URL's databasepassword
HEADER_KEY="your-secret-key-here" # used in custom header
HEADER_VALUE="your-secret-key-here" # used in custom header
31 changes: 31 additions & 0 deletions backend/auth.ts
@@ -0,0 +1,31 @@
import { PrismaAdapter } from '@lucia-auth/adapter-prisma';
import { Lucia } from 'lucia';
import prisma from '~/prisma';
import { webcrypto } from 'crypto';
import type { User } from '~/shared/types';

globalThis.crypto = webcrypto as Crypto;

const adapter = new PrismaAdapter(prisma.session, prisma.user);

export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: process.env.NODE_ENV === 'production',
},
},
getUserAttributes(attributes: Partial<User>) {
return {
email: attributes.email,
};
},
});

declare module 'lucia' {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}

interface DatabaseUserAttributes extends User {}
32 changes: 32 additions & 0 deletions middleware.ts
@@ -0,0 +1,32 @@
import { verifyRequestOrigin } from 'lucia';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export const CUSTOM_HEADER = {
key: process.env.HEADER_KEY,
value: process.env.HEADER_VALUE,
} as const;

//eslint-disable-next-line
export async function middleware(req: NextRequest): Promise<NextResponse> {
if (req.method === 'GET') {
return NextResponse.next();
}
const originHeader = req.headers.get('Origin');
const hostHeader = req.headers.get('Host');

if (CUSTOM_HEADER.key !== undefined && CUSTOM_HEADER.value !== undefined) {
if (req.headers.get(CUSTOM_HEADER.key) === CUSTOM_HEADER.value) {
return NextResponse.next();
}
}

if (
!originHeader ||
!hostHeader ||
!verifyRequestOrigin(originHeader, [hostHeader])
) {
return new NextResponse(null, { status: 403 });
}
return NextResponse.next();
}