Logging in via github should work even after changing your email address
This commit is contained in:
@@ -78,27 +78,72 @@ export async function findOrCreateGithubUser({
|
||||
? (authenticationExtraParams as unknown as Prisma.JsonObject)
|
||||
: undefined;
|
||||
|
||||
const fields = {
|
||||
accessToken,
|
||||
authenticationProfile: authProfile,
|
||||
authenticationExtraParams: authExtraParams,
|
||||
name,
|
||||
avatarUrl,
|
||||
displayName,
|
||||
};
|
||||
const authIdentifier = `github:${authenticationProfile.id}`;
|
||||
|
||||
const existingUser = await prisma.user.findFirst({
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: {
|
||||
authIdentifier,
|
||||
},
|
||||
});
|
||||
|
||||
const existingEmailUser = await prisma.user.findUnique({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingEmailUser && !existingUser) {
|
||||
const user = await prisma.user.update({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
data: {
|
||||
accessToken,
|
||||
authenticationProfile: authProfile,
|
||||
authenticationExtraParams: authExtraParams,
|
||||
avatarUrl,
|
||||
authIdentifier,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
user,
|
||||
isNewUser: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (existingEmailUser && existingUser) {
|
||||
const user = await prisma.user.update({
|
||||
where: {
|
||||
id: existingUser.id,
|
||||
},
|
||||
data: {
|
||||
accessToken,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
user,
|
||||
isNewUser: false,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.upsert({
|
||||
where: {
|
||||
email,
|
||||
authIdentifier,
|
||||
},
|
||||
update: { accessToken },
|
||||
create: {
|
||||
accessToken,
|
||||
authenticationProfile: authProfile,
|
||||
authenticationExtraParams: authExtraParams,
|
||||
name,
|
||||
avatarUrl,
|
||||
displayName,
|
||||
authIdentifier,
|
||||
email,
|
||||
authenticationMethod: "GITHUB",
|
||||
},
|
||||
update: fields,
|
||||
create: { ...fields, email, authenticationMethod: "GITHUB" },
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
import type { LoaderFunction } from "@remix-run/node";
|
||||
import { authenticator } from "~/services/auth.server";
|
||||
import { redirectCookie } from "./auth.github";
|
||||
import { logger } from "~/services/logger.server";
|
||||
|
||||
export let loader: LoaderFunction = async ({ request }) => {
|
||||
const cookie = request.headers.get("Cookie");
|
||||
const redirectValue = await redirectCookie.parse(cookie);
|
||||
const redirectTo = redirectValue ?? "/";
|
||||
|
||||
logger.debug("auth.github.callback loader", {
|
||||
redirectTo,
|
||||
});
|
||||
|
||||
const authuser = await authenticator.authenticate("github", request, {
|
||||
successRedirect: redirectTo,
|
||||
failureRedirect: "/login",
|
||||
});
|
||||
|
||||
logger.debug("auth.github.callback authuser", {
|
||||
authuser,
|
||||
});
|
||||
|
||||
return authuser;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import { env } from "~/env.server";
|
||||
import { findOrCreateUser } from "~/models/user.server";
|
||||
import type { AuthUser } from "./authUser";
|
||||
import { postAuthentication } from "./postAuth.server";
|
||||
import { logger } from "./logger.server";
|
||||
|
||||
export function addGitHubStrategy(
|
||||
authenticator: Authenticator<AuthUser>,
|
||||
@@ -24,6 +25,12 @@ export function addGitHubStrategy(
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug("GitHub login", {
|
||||
emails,
|
||||
profile,
|
||||
extraParams,
|
||||
});
|
||||
|
||||
const { user, isNewUser } = await findOrCreateUser({
|
||||
email: emails[0].value,
|
||||
authenticationMethod: "GITHUB",
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "User" ADD COLUMN "authIdentifier" TEXT;
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- A unique constraint covering the columns `[authIdentifier]` on the table `User` will be added. If there are existing duplicate values, this will fail.
|
||||
|
||||
*/
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_authIdentifier_key" ON "User"("authIdentifier");
|
||||
@@ -16,6 +16,7 @@ model User {
|
||||
accessToken String?
|
||||
authenticationProfile Json?
|
||||
authenticationExtraParams Json?
|
||||
authIdentifier String? @unique
|
||||
|
||||
displayName String?
|
||||
name String?
|
||||
|
||||
Reference in New Issue
Block a user