Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/dashboard/app/auth/sign-in/oauth-signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const OAuthSignIn: React.FC = () => {
try {
setIsLoading(provider);
setLastUsed(provider);

const url = await signInViaOAuth({
provider,
redirectUrlComplete,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { setCookiesOnResponse } from "@/lib/auth/cookies";
import { auth } from "@/lib/auth/server";
import { AuthErrorCode, SIGN_IN_URL } from "@/lib/auth/types";
import { getBaseUrl } from "@/lib/utils";
import { type NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
const authResult = await auth.completeOAuthSignIn(request);
Expand Down Expand Up @@ -34,7 +35,7 @@ export async function GET(request: NextRequest) {
}

// Get base URL from request because Next.js wants it
const baseUrl = new URL(request.url).origin;
const baseUrl = getBaseUrl();
const response = NextResponse.redirect(new URL(authResult.redirectTo, baseUrl));

return await setCookiesOnResponse(response, authResult.cookies);
Expand Down
1 change: 0 additions & 1 deletion apps/dashboard/lib/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export interface SessionData {
export type OAuthStrategy = "google" | "github";

export interface SignInViaOAuthOptions {
redirectUrl?: string;
redirectUrlComplete: string;
provider: OAuthStrategy;
}
Expand Down
27 changes: 20 additions & 7 deletions apps/dashboard/lib/auth/workos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type Invitation as WorkOSInvitation,
type Organization as WorkOSOrganization,
} from "@workos-inc/node";
import { getBaseUrl } from "../utils";
import { BaseAuthProvider } from "./base-provider";
import { getCookie } from "./cookies";
import {
Expand Down Expand Up @@ -192,7 +193,10 @@ export class WorkOSAuthProvider extends BaseAuthProvider {
}

// Organization Management
async createTenant(params: { name: string; userId: string }): Promise<string> {
async createTenant(params: {
name: string;
userId: string;
}): Promise<string> {
const { name, userId } = params;
if (!name || !userId) {
throw new Error("Organization name and userId are required.");
Expand Down Expand Up @@ -220,7 +224,9 @@ export class WorkOSAuthProvider extends BaseAuthProvider {
}

try {
const org = await this.provider.organizations.createOrganization({ name });
const org = await this.provider.organizations.createOrganization({
name,
});
return this.transformOrganizationData(org);
} catch (error) {
throw this.handleError(error);
Expand Down Expand Up @@ -453,7 +459,10 @@ export class WorkOSAuthProvider extends BaseAuthProvider {
inviterUserId: user.id,
});

return this.transformInvitationData(invitation, { orgId, inviterId: user.id });
return this.transformInvitationData(invitation, {
orgId,
inviterId: user.id,
});
} catch (error) {
throw this.handleError(error);
}
Expand Down Expand Up @@ -643,7 +652,10 @@ export class WorkOSAuthProvider extends BaseAuthProvider {
}
}

async verifyEmail(params: { code: string; token: string }): Promise<VerificationResult> {
async verifyEmail(params: {
code: string;
token: string;
}): Promise<VerificationResult> {
const { code, token } = params;

try {
Expand Down Expand Up @@ -767,12 +779,13 @@ export class WorkOSAuthProvider extends BaseAuthProvider {

// OAuth Methods
signInViaOAuth(options: SignInViaOAuthOptions): string {
const { redirectUrl, provider, redirectUrlComplete } = options;
const { provider, redirectUrlComplete } = options;
const state = encodeURIComponent(JSON.stringify({ redirectUrlComplete }));

const baseUrl = getBaseUrl();
const redirect = `${baseUrl}/auth/sso-callback`;
return this.provider.userManagement.getAuthorizationUrl({
clientId: this.clientId,
redirectUri: redirectUrl ?? env().NEXT_PUBLIC_WORKOS_REDIRECT_URI,
redirectUri: env().NEXT_PUBLIC_WORKOS_REDIRECT_URI ?? redirect,
provider: provider === "github" ? "GitHubOAuth" : "GoogleOAuth",
state,
});
Expand Down
16 changes: 1 addition & 15 deletions apps/dashboard/lib/trpc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@ import { createTRPCProxyClient, httpLink } from "@trpc/client";
import superjson from "superjson";

import type { Router } from "./routers";

function getBaseUrl() {
if (typeof window !== "undefined") {
// browser should use relative path
return "";
}

if (process.env.VERCEL_URL) {
// reference for vercel.com
return `https://${process.env.VERCEL_URL}`;
}

// assume localhost
return `http://localhost:${process.env.PORT ?? 3000}`;
}
import { getBaseUrl } from "../utils";

export const trpc = createTRPCProxyClient<Router>({
transformer: superjson,
Expand Down
15 changes: 15 additions & 0 deletions apps/dashboard/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,18 @@ export const processTimeFilters = (date?: Date, newTime?: TimeUnit) => {
const now = new Date();
return now;
};

export function getBaseUrl() {
if (typeof window !== "undefined") {
// browser should use relative path
return "";
}

if (process.env.VERCEL_URL) {
// reference for vercel.com
return `https://${process.env.VERCEL_URL}`;
}

// assume localhost
return `http://localhost:${process.env.PORT ?? 3000}`;
}
Loading