@@ -5,21 +5,17 @@ import EmailProvider from "next-auth/providers/nodemailer";
55import { PrismaAdapter } from "@auth/prisma-adapter"
66import { prisma } from "@/prisma" ;
77import { env } from "@/env.mjs" ;
8- import { OrgRole , User } from '@sourcebot/db' ;
8+ import { User } from '@sourcebot/db' ;
99import 'next-auth/jwt' ;
1010import type { Provider } from "next-auth/providers" ;
1111import { verifyCredentialsRequestSchema } from './lib/schemas' ;
1212import { createTransport } from 'nodemailer' ;
1313import { render } from '@react-email/render' ;
1414import MagicLinkEmail from './emails/magicLinkEmail' ;
15- import { SINGLE_TENANT_ORG_DOMAIN , SINGLE_TENANT_ORG_ID } from './lib/constants' ;
1615import bcrypt from 'bcryptjs' ;
17- import { createAccountRequest } from './actions' ;
18- import { getSSOProviders , handleJITProvisioning } from '@/ee/sso/sso' ;
16+ import { getSSOProviders } from '@/ee/sso/sso' ;
1917import { hasEntitlement } from '@/features/entitlements/server' ;
20- import { isServiceError } from './lib/utils' ;
21- import { ServiceErrorException } from './lib/serviceError' ;
22- import { createLogger } from "@sourcebot/logger" ;
18+ import { onCreateUser } from '@/lib/authUtils' ;
2319
2420export const runtime = 'nodejs' ;
2521
@@ -37,8 +33,6 @@ declare module 'next-auth/jwt' {
3733 }
3834}
3935
40- const logger = createLogger ( 'web-auth' ) ;
41-
4236export const getProviders = ( ) => {
4337 const providers : Provider [ ] = [ ] ;
4438
@@ -134,91 +128,6 @@ export const getProviders = () => {
134128 return providers ;
135129}
136130
137- const onCreateUser = async ( { user } : { user : AuthJsUser } ) => {
138- // In single-tenant mode, we assign the first user to sign
139- // up as the owner of the default org.
140- if (
141- env . SOURCEBOT_TENANCY_MODE === 'single'
142- ) {
143- const defaultOrg = await prisma . org . findUnique ( {
144- where : {
145- id : SINGLE_TENANT_ORG_ID ,
146- } ,
147- include : {
148- members : {
149- where : {
150- role : {
151- not : OrgRole . GUEST ,
152- }
153- }
154- } ,
155- }
156- } ) ;
157-
158- if ( ! defaultOrg ) {
159- throw new Error ( "Default org not found on single tenant user creation" ) ;
160- }
161-
162- // We can't use the getOrgMembers action here because we're not authed yet
163- const members = await prisma . userToOrg . findMany ( {
164- where : {
165- orgId : SINGLE_TENANT_ORG_ID ,
166- role : {
167- not : OrgRole . GUEST ,
168- }
169- } ,
170- } ) ;
171-
172- // Only the first user to sign up will be an owner of the default org.
173- const isFirstUser = members . length === 0 ;
174- if ( isFirstUser ) {
175- await prisma . $transaction ( async ( tx ) => {
176- await tx . org . update ( {
177- where : {
178- id : SINGLE_TENANT_ORG_ID ,
179- } ,
180- data : {
181- members : {
182- create : {
183- role : OrgRole . OWNER ,
184- user : {
185- connect : {
186- id : user . id ,
187- }
188- }
189- }
190- }
191- }
192- } ) ;
193-
194- await tx . user . update ( {
195- where : {
196- id : user . id ,
197- } ,
198- data : {
199- pendingApproval : false ,
200- }
201- } ) ;
202- } ) ;
203- } else {
204- // TODO(auth): handle multi tenant case
205- if ( env . AUTH_EE_ENABLE_JIT_PROVISIONING === 'true' && hasEntitlement ( "sso" ) ) {
206- const res = await handleJITProvisioning ( user . id ! , SINGLE_TENANT_ORG_DOMAIN ) ;
207- if ( isServiceError ( res ) ) {
208- logger . error ( `Failed to provision user ${ user . id } for org ${ SINGLE_TENANT_ORG_DOMAIN } : ${ res . message } ` ) ;
209- throw new ServiceErrorException ( res ) ;
210- }
211- } else {
212- const res = await createAccountRequest ( user . id ! , SINGLE_TENANT_ORG_DOMAIN ) ;
213- if ( isServiceError ( res ) ) {
214- logger . error ( `Failed to provision user ${ user . id } for org ${ SINGLE_TENANT_ORG_DOMAIN } : ${ res . message } ` ) ;
215- throw new ServiceErrorException ( res ) ;
216- }
217- }
218- }
219- }
220- }
221-
222131export const { handlers, signIn, signOut, auth } = NextAuth ( {
223132 secret : env . AUTH_SECRET ,
224133 adapter : PrismaAdapter ( prisma ) ,
0 commit comments