-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from goodeats/108-prevent-new-users-after-dep…
…loyment prevent new users after deployment
- Loading branch information
Showing
61 changed files
with
770 additions
and
389 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,179 +1,162 @@ | ||
import { redirect, type LoaderFunctionArgs } from '@remix-run/node' | ||
import { | ||
authenticator, | ||
getSessionExpirationDate, | ||
getUserId, | ||
} from '#app/utils/auth.server.ts' | ||
import { ProviderNameSchema, providerLabels } from '#app/utils/connections.tsx' | ||
import { prisma } from '#app/utils/db.server.ts' | ||
import { combineHeaders } from '#app/utils/misc.tsx' | ||
import { | ||
destroyRedirectToHeader, | ||
getRedirectCookieValue, | ||
} from '#app/utils/redirect-cookie.server.ts' | ||
import { | ||
createToastHeaders, | ||
redirectWithToast, | ||
} from '#app/utils/toast.server.ts' | ||
import { verifySessionStorage } from '#app/utils/verification.server.ts' | ||
import { handleNewSession } from './login.server.ts' | ||
import { | ||
onboardingEmailSessionKey, | ||
prefilledProfileKey, | ||
providerIdKey, | ||
} from './onboarding_.$provider.tsx' | ||
import { type LoaderFunctionArgs } from '@remix-run/node' | ||
import { redirectWithToast } from '#app/utils/toast.server.ts' | ||
|
||
const destroyRedirectTo = { 'set-cookie': destroyRedirectToHeader } | ||
// const destroyRedirectTo = { 'set-cookie': destroyRedirectToHeader } | ||
|
||
export async function loader({ request, params }: LoaderFunctionArgs) { | ||
const providerName = ProviderNameSchema.parse(params.provider) | ||
const redirectTo = getRedirectCookieValue(request) | ||
const label = providerLabels[providerName] | ||
return redirectWithToast('/', { | ||
type: 'error', | ||
title: 'Access Denied', | ||
description: 'No new users at this time.', | ||
}) | ||
// const providerName = ProviderNameSchema.parse(params.provider) | ||
// const redirectTo = getRedirectCookieValue(request) | ||
// const label = providerLabels[providerName] | ||
|
||
const authResult = await authenticator | ||
.authenticate(providerName, request, { throwOnError: true }) | ||
.then( | ||
data => ({ success: true, data }) as const, | ||
error => ({ success: false, error }) as const, | ||
) | ||
// const authResult = await authenticator | ||
// .authenticate(providerName, request, { throwOnError: true }) | ||
// .then( | ||
// data => ({ success: true, data }) as const, | ||
// error => ({ success: false, error }) as const, | ||
// ) | ||
|
||
if (!authResult.success) { | ||
console.error(authResult.error) | ||
throw await redirectWithToast( | ||
'/login', | ||
{ | ||
title: 'Auth Failed', | ||
description: `There was an error authenticating with ${label}.`, | ||
type: 'error', | ||
}, | ||
{ headers: destroyRedirectTo }, | ||
) | ||
} | ||
// if (!authResult.success) { | ||
// console.error(authResult.error) | ||
// throw await redirectWithToast( | ||
// '/login', | ||
// { | ||
// title: 'Auth Failed', | ||
// description: `There was an error authenticating with ${label}.`, | ||
// type: 'error', | ||
// }, | ||
// { headers: destroyRedirectTo }, | ||
// ) | ||
// } | ||
|
||
const { data: profile } = authResult | ||
// const { data: profile } = authResult | ||
|
||
const existingConnection = await prisma.connection.findUnique({ | ||
select: { userId: true }, | ||
where: { | ||
providerName_providerId: { providerName, providerId: profile.id }, | ||
}, | ||
}) | ||
// const existingConnection = await prisma.connection.findUnique({ | ||
// select: { userId: true }, | ||
// where: { | ||
// providerName_providerId: { providerName, providerId: profile.id }, | ||
// }, | ||
// }) | ||
|
||
const userId = await getUserId(request) | ||
// const userId = await getUserId(request) | ||
|
||
if (existingConnection && userId) { | ||
if (existingConnection.userId === userId) { | ||
return redirectWithToast( | ||
'/settings/profile/connections', | ||
{ | ||
title: 'Already Connected', | ||
description: `Your "${profile.username}" ${label} account is already connected.`, | ||
}, | ||
{ headers: destroyRedirectTo }, | ||
) | ||
} else { | ||
return redirectWithToast( | ||
'/settings/profile/connections', | ||
{ | ||
title: 'Already Connected', | ||
description: `The "${profile.username}" ${label} account is already connected to another account.`, | ||
}, | ||
{ headers: destroyRedirectTo }, | ||
) | ||
} | ||
} | ||
// if (existingConnection && userId) { | ||
// if (existingConnection.userId === userId) { | ||
// return redirectWithToast( | ||
// '/settings/profile/connections', | ||
// { | ||
// title: 'Already Connected', | ||
// description: `Your "${profile.username}" ${label} account is already connected.`, | ||
// }, | ||
// { headers: destroyRedirectTo }, | ||
// ) | ||
// } else { | ||
// return redirectWithToast( | ||
// '/settings/profile/connections', | ||
// { | ||
// title: 'Already Connected', | ||
// description: `The "${profile.username}" ${label} account is already connected to another account.`, | ||
// }, | ||
// { headers: destroyRedirectTo }, | ||
// ) | ||
// } | ||
// } | ||
|
||
// If we're already logged in, then link the account | ||
if (userId) { | ||
await prisma.connection.create({ | ||
data: { | ||
providerName, | ||
providerId: profile.id, | ||
userId, | ||
}, | ||
}) | ||
return redirectWithToast( | ||
'/settings/profile/connections', | ||
{ | ||
title: 'Connected', | ||
type: 'success', | ||
description: `Your "${profile.username}" ${label} account has been connected.`, | ||
}, | ||
{ headers: destroyRedirectTo }, | ||
) | ||
} | ||
// // If we're already logged in, then link the account | ||
// if (userId) { | ||
// await prisma.connection.create({ | ||
// data: { | ||
// providerName, | ||
// providerId: profile.id, | ||
// userId, | ||
// }, | ||
// }) | ||
// return redirectWithToast( | ||
// '/settings/profile/connections', | ||
// { | ||
// title: 'Connected', | ||
// type: 'success', | ||
// description: `Your "${profile.username}" ${label} account has been connected.`, | ||
// }, | ||
// { headers: destroyRedirectTo }, | ||
// ) | ||
// } | ||
|
||
// Connection exists already? Make a new session | ||
if (existingConnection) { | ||
return makeSession({ request, userId: existingConnection.userId }) | ||
} | ||
// // Connection exists already? Make a new session | ||
// if (existingConnection) { | ||
// return makeSession({ request, userId: existingConnection.userId }) | ||
// } | ||
|
||
// if the email matches a user in the db, then link the account and | ||
// make a new session | ||
const user = await prisma.user.findUnique({ | ||
select: { id: true }, | ||
where: { email: profile.email.toLowerCase() }, | ||
}) | ||
if (user) { | ||
await prisma.connection.create({ | ||
data: { | ||
providerName, | ||
providerId: profile.id, | ||
userId: user.id, | ||
}, | ||
}) | ||
return makeSession( | ||
{ request, userId: user.id }, | ||
{ | ||
headers: await createToastHeaders({ | ||
title: 'Connected', | ||
description: `Your "${profile.username}" ${label} account has been connected.`, | ||
}), | ||
}, | ||
) | ||
} | ||
// // if the email matches a user in the db, then link the account and | ||
// // make a new session | ||
// const user = await prisma.user.findUnique({ | ||
// select: { id: true }, | ||
// where: { email: profile.email.toLowerCase() }, | ||
// }) | ||
// if (user) { | ||
// await prisma.connection.create({ | ||
// data: { | ||
// providerName, | ||
// providerId: profile.id, | ||
// userId: user.id, | ||
// }, | ||
// }) | ||
// return makeSession( | ||
// { request, userId: user.id }, | ||
// { | ||
// headers: await createToastHeaders({ | ||
// title: 'Connected', | ||
// description: `Your "${profile.username}" ${label} account has been connected.`, | ||
// }), | ||
// }, | ||
// ) | ||
// } | ||
|
||
// this is a new user, so let's get them onboarded | ||
const verifySession = await verifySessionStorage.getSession() | ||
verifySession.set(onboardingEmailSessionKey, profile.email) | ||
verifySession.set(prefilledProfileKey, { | ||
...profile, | ||
email: profile.email.toLowerCase(), | ||
username: profile.username?.replace(/[^a-zA-Z0-9_]/g, '_').toLowerCase(), | ||
}) | ||
verifySession.set(providerIdKey, profile.id) | ||
const onboardingRedirect = [ | ||
`/onboarding/${providerName}`, | ||
redirectTo ? new URLSearchParams({ redirectTo }) : null, | ||
] | ||
.filter(Boolean) | ||
.join('?') | ||
return redirect(onboardingRedirect, { | ||
headers: combineHeaders( | ||
{ 'set-cookie': await verifySessionStorage.commitSession(verifySession) }, | ||
destroyRedirectTo, | ||
), | ||
}) | ||
// // this is a new user, so let's get them onboarded | ||
// const verifySession = await verifySessionStorage.getSession() | ||
// verifySession.set(onboardingEmailSessionKey, profile.email) | ||
// verifySession.set(prefilledProfileKey, { | ||
// ...profile, | ||
// email: profile.email.toLowerCase(), | ||
// username: profile.username?.replace(/[^a-zA-Z0-9_]/g, '_').toLowerCase(), | ||
// }) | ||
// verifySession.set(providerIdKey, profile.id) | ||
// const onboardingRedirect = [ | ||
// `/onboarding/${providerName}`, | ||
// redirectTo ? new URLSearchParams({ redirectTo }) : null, | ||
// ] | ||
// .filter(Boolean) | ||
// .join('?') | ||
// return redirect(onboardingRedirect, { | ||
// headers: combineHeaders( | ||
// { 'set-cookie': await verifySessionStorage.commitSession(verifySession) }, | ||
// destroyRedirectTo, | ||
// ), | ||
// }) | ||
} | ||
|
||
async function makeSession( | ||
{ | ||
request, | ||
userId, | ||
redirectTo, | ||
}: { request: Request; userId: string; redirectTo?: string | null }, | ||
responseInit?: ResponseInit, | ||
) { | ||
redirectTo ??= '/' | ||
const session = await prisma.session.create({ | ||
select: { id: true, expirationDate: true, userId: true }, | ||
data: { | ||
expirationDate: getSessionExpirationDate(), | ||
userId, | ||
}, | ||
}) | ||
return handleNewSession( | ||
{ request, session, redirectTo, remember: true }, | ||
{ headers: combineHeaders(responseInit?.headers, destroyRedirectTo) }, | ||
) | ||
} | ||
// async function makeSession( | ||
// { | ||
// request, | ||
// userId, | ||
// redirectTo, | ||
// }: { request: Request; userId: string; redirectTo?: string | null }, | ||
// responseInit?: ResponseInit, | ||
// ) { | ||
// redirectTo ??= '/' | ||
// const session = await prisma.session.create({ | ||
// select: { id: true, expirationDate: true, userId: true }, | ||
// data: { | ||
// expirationDate: getSessionExpirationDate(), | ||
// userId, | ||
// }, | ||
// }) | ||
// return handleNewSession( | ||
// { request, session, redirectTo, remember: true }, | ||
// { headers: combineHeaders(responseInit?.headers, destroyRedirectTo) }, | ||
// ) | ||
// } |
Oops, something went wrong.