Skip to content

Commit

Permalink
fix: NextAuth Error (#3516)
Browse files Browse the repository at this point in the history
* fix: NextAuth Error

* fix: clean logs

* fix: NextAuth Error

* refactor: change default value of dev Auth Secret
  • Loading branch information
Cedric921 authored Jan 15, 2025
1 parent c07536b commit 95b8343
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
25 changes: 2 additions & 23 deletions apps/web/.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ NEXT_PUBLIC_GA_MEASUREMENT_ID=
# Next Auth configs
AUTH_SECRET=

IS_DESKTOP_APP=

# Google OAuth configuration
NEXT_PUBLIC_GOOGLE_APP_NAME=ever-google
GOOGLE_CLIENT_ID=
Expand All @@ -35,29 +37,6 @@ TWITTER_CLIENT_SECRET=
NEXT_PUBLIC_CAPTCHA_SITE_KEY=
CAPTCHA_SECRET_KEY=

# Next Auth configs
AUTH_SECRET=

# Google OAuth configuration
NEXT_PUBLIC_GOOGLE_APP_NAME=ever-google
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

# Github OAuth configuration
NEXT_PUBLIC_GITHUB_APP_NAME=ever-github
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=

# Facebook OAuth configuration
NEXT_PUBLIC_FACEBOOK_APP_NAME=ever-facebook
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=

# Twitter OAuth configuration
NEXT_PUBLIC_TWITTER_APP_NAME=ever-twitter
TWITTER_CLIENT_ID=
TWITTER_CLIENT_SECRET=

# MEET_TYPE: LiveKit | Jitsi
NEXT_PUBLIC_MEET_TYPE=Jitsi

Expand Down
7 changes: 6 additions & 1 deletion apps/web/app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const RECAPTCHA_SITE_KEY = getNextPublicEnv(
export const RECAPTCHA_SECRET_KEY = process.env.CAPTCHA_SECRET_KEY;
let basePath = process.env.GAUZY_API_SERVER_URL ? process.env.GAUZY_API_SERVER_URL : 'https://api.ever.team';
if (IS_DESKTOP_APP) {
const serverRuntimeConfig = getServerRuntimeConfig()
const serverRuntimeConfig = getServerRuntimeConfig();
basePath = serverRuntimeConfig?.GAUZY_API_SERVER_URL || basePath;
}

Expand Down Expand Up @@ -299,6 +299,8 @@ export const HAS_SEEN_DAILY_PLAN_SUGGESTION_MODAL = 'has-seen-daily-plan-suggest

// OAuth provider's keys

export const AUTH_SECRET = process.env.AUTH_SECRET;

export const APPLE_CLIENT_ID = process.env.APPLE_CLIENT_ID;
export const APPLE_CLIENT_SECRET = process.env.APPLE_CLIENT_SECRET;

Expand Down Expand Up @@ -326,6 +328,9 @@ export const SLACK_CLIENT_SECRET = process.env.SLACK_CLIENT_SECRET;
export const TWITTER_CLIENT_ID = process.env.TWITTER_CLIENT_ID;
export const TWITTER_CLIENT_SECRET = process.env.TWITTER_CLIENT_SECRET;

export const developmentAuthSecret = 'DEFAULT_VALUE_OF_SECRET_FOR_DEVELOPMENT';
export const isDevelopment = process.env.NODE_ENV === 'development';

// Add manual timer reason

export const manualTimeReasons: ManualTimeReasons[] = [
Expand Down
29 changes: 21 additions & 8 deletions apps/web/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@ import NextAuth from 'next-auth';
import { filteredProviders } from '@app/utils/check-provider-env-vars';
import { GauzyAdapter, jwtCallback, ProviderEnum, signInCallback } from '@app/services/server/requests/OAuth';
import { NextRequest } from 'next/server';
import { IS_DESKTOP_APP } from '@app/constants';
import { AUTH_SECRET, IS_DESKTOP_APP, developmentAuthSecret, isDevelopment } from '@app/constants';

const secretKey = AUTH_SECRET || (isDevelopment ? developmentAuthSecret : '');

if (!secretKey) {
console.warn('Missing secret: Please define AUTH_SECRET in the environment variables.');
}

export const { handlers, signIn, signOut, auth } = NextAuth((request) => ({
providers: filteredProviders,
trustHost: IS_DESKTOP_APP,
trustHost: IS_DESKTOP_APP || process.env.NODE_ENV === 'production',
secret: secretKey,
debug: process.env.NODE_ENV === 'development',
adapter: GauzyAdapter(request as NextRequest),
session: { strategy: 'jwt' },
callbacks: {
async signIn({ account }) {
if (account) {
const { provider, access_token } = account;
if (access_token) {
await signInCallback(provider as ProviderEnum, access_token);
try {
if (account) {
const { provider, access_token } = account;
if (access_token) {
await signInCallback(provider as ProviderEnum, access_token);
return true;
}
return true;
}
return true;
return false;
} catch (error) {
console.error('Error in signIn callback:', error);
return false;
}
return false;
},

async jwt({ token, user, trigger, session, account }) {
Expand Down

0 comments on commit 95b8343

Please sign in to comment.