Skip to content

Commit

Permalink
First member of an organization should be owner (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Mar 23, 2024
1 parent 2adf098 commit b2436d3
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions apps/platform/src/auth/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,38 @@ export default abstract class AuthProvider {
abstract start(ctx: AuthContext): Promise<void>
abstract validate(ctx: AuthContext): Promise<void>

async loadAuthOrganization(ctx: AuthContext, domain?: string) {
async loadAuthOrganization(ctx: AuthContext, domain?: string): Promise<{ organization: Organization, isNew: boolean }> {

// If we have an organization or can find one by domain
// we use that to start
let organization = ctx.state.organization ?? await getOrganizationByDomain(domain)
if (organization) return organization
if (organization) return { organization, isNew: false }

// If we are not in multi-org mode we always fall back to
// a single organization
if (!App.main.env.config.multiOrg) {
organization = await getDefaultOrganization()
}
if (organization) return organization
if (organization) return { organization, isNew: false }

// If there is no organization at all or are in multi-org mode
// and have no org for the user, create one
return await createOrganization(domain)
return {
organization: await createOrganization(domain),
isNew: true,
}
}

async login({ domain, ...params }: AuthAdminParams, ctx: AuthContext, redirect?: string): Promise<OAuthResponse> {

// Check for existing, otherwise create one
let admin = await getAdminByEmail(params.email)
if (!admin) {
const organization = await this.loadAuthOrganization(ctx, domain)
const { organization, isNew } = await this.loadAuthOrganization(ctx, domain)
admin = await Admin.insertAndFetch({
...params,
organization_id: organization.id,
role: isNew ? 'owner' : 'member',
})
}

Expand Down

0 comments on commit b2436d3

Please sign in to comment.