-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bump a lot of deps * Adjust docker build tag generation * Use branch and pr as ref value for tag creation * Do not optimize images * Adjust discord provider config for auth.js * Add more error handling to auth.ts * Display correct error message
- Loading branch information
Showing
98 changed files
with
2,493 additions
and
1,843 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { handlers } from '../../../../auth' | ||
|
||
export const { GET, POST } = handlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import NextAuth from 'next-auth' | ||
import Discord from 'next-auth/providers/discord' | ||
import { addUser } from './lib/db/users' | ||
import { AccountType, type User } from './types/User' | ||
import { MongoDBAdapter } from '@auth/mongodb-adapter' | ||
import { dbClient } from './lib/db/db' | ||
import { ObjectId } from 'mongodb' | ||
import type { NextAuthConfig } from 'next-auth' | ||
import { findOneTyped } from './lib/db/dbTyped' | ||
import { Types } from './types/Components' | ||
|
||
export const authOptions: NextAuthConfig = { | ||
providers: [Discord], | ||
theme: { | ||
colorScheme: 'dark', | ||
brandColor: '#0d6efd', | ||
logo: process.env.NEXT_PUBLIC_DOMAIN + '/icons/logo.png', | ||
}, | ||
callbacks: { | ||
// we want to access the user id | ||
async session({ session, user }) { | ||
if (user) { | ||
const id = user.id.toString() | ||
session.user.uid = id | ||
|
||
const userData = (await findOneTyped(Types.user, id)) as User | ||
// create user account if not found | ||
if (userData === null) { | ||
console.log( | ||
'User', | ||
user.name, | ||
id, | ||
'could not be found, creating new user' | ||
) | ||
await addUser({ | ||
uid: id, | ||
accountType: AccountType.user, | ||
}).catch((error) => { | ||
console.error( | ||
'Failed to add new user in session callback', | ||
user, | ||
'due to', | ||
error | ||
) | ||
}) | ||
|
||
session.user.accountType = AccountType.user | ||
} else { | ||
session.user.accountType = userData.accountType | ||
} | ||
} else { | ||
console.warn('session call with no user provided') | ||
} | ||
return session | ||
}, | ||
}, | ||
events: { | ||
async signIn({ user, account, profile, isNewUser }) { | ||
if (typeof user === 'undefined' || user === null) { | ||
console.error('Sign In event with no user provided:', user) | ||
return | ||
} | ||
|
||
if (isNewUser) { | ||
console.log('Creating new user', user.name) | ||
const accountType = | ||
typeof account !== 'undefined' && | ||
account !== null && | ||
typeof process.env.SETUP_WHITELIST_DISCORD_ID !== 'undefined' && | ||
process.env.SETUP_WHITELIST_DISCORD_ID !== '' && | ||
account.providerAccountId === process.env.SETUP_WHITELIST_DISCORD_ID | ||
? AccountType.admin | ||
: AccountType.user | ||
if (typeof user.id !== 'undefined') { | ||
await addUser({ | ||
uid: user.id.toString(), | ||
accountType, | ||
}).catch((error) => { | ||
console.error('Failed to add new user', user, 'due to', error) | ||
}) | ||
} else { | ||
console.error('Unable to create new user', user, 'due to missing id') | ||
} | ||
} else if ( | ||
typeof profile !== 'undefined' && | ||
user.image !== profile.image | ||
) { | ||
if (typeof user.id !== 'undefined') { | ||
// update new discord image on login | ||
const db = (await dbClient).db('index') | ||
await db | ||
.collection('nextauth_users') | ||
.updateOne( | ||
{ _id: new ObjectId(user.id) }, | ||
{ | ||
$set: { | ||
image: profile.image, | ||
}, | ||
} | ||
) | ||
.catch((error) => { | ||
console.error( | ||
'Unable to update profile image of user', | ||
user, | ||
'due to', | ||
error | ||
) | ||
}) | ||
} else { | ||
console.log( | ||
'Unable to update user profile picutre', | ||
user, | ||
'due to missing id' | ||
) | ||
} | ||
} | ||
}, | ||
}, | ||
|
||
// A database is optional, but required to persist accounts in a database | ||
adapter: MongoDBAdapter(dbClient, { | ||
collections: { | ||
Users: 'nextauth_users', | ||
Sessions: 'nextauth_sessions', | ||
Accounts: 'nextauth_accounts', | ||
}, | ||
}), | ||
secret: process.env.NEXTAUTH_SECRET, | ||
} | ||
|
||
export const { auth, handlers, signIn, signOut } = NextAuth(authOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ const Card: FC<Props> = ({ | |
alt='...' | ||
width={128} | ||
height={128} | ||
unoptimized | ||
/> | ||
</Link> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.