Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 71253c9a01 |
@@ -12,6 +12,7 @@ export const FacebookUserDataSchema = z
|
|||||||
.object({
|
.object({
|
||||||
id: stringOrNull,
|
id: stringOrNull,
|
||||||
name: stringOrNull,
|
name: stringOrNull,
|
||||||
|
email: stringOrNull,
|
||||||
pictureUrl: stringOrNull,
|
pictureUrl: stringOrNull,
|
||||||
})
|
})
|
||||||
.readonly();
|
.readonly();
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
import { fetchFacebookUserData } from "../facebook-graph";
|
|
||||||
|
|
||||||
describe("fetchFacebookUserData", () => {
|
|
||||||
afterEach(() => {
|
|
||||||
vi.unstubAllGlobals();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("requests profile data without requesting or returning email", async () => {
|
|
||||||
const fetchMock = vi.fn().mockResolvedValue(
|
|
||||||
new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
id: "facebook-1",
|
|
||||||
name: "Elio",
|
|
||||||
email: "should-not-be-used@example.com",
|
|
||||||
picture: {
|
|
||||||
data: { url: "https://example.com/avatar.jpg" },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 200,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
vi.stubGlobal("fetch", fetchMock);
|
|
||||||
|
|
||||||
const result = await fetchFacebookUserData("access-token");
|
|
||||||
const requestUrl = new URL(String(fetchMock.mock.calls[0]?.[0]));
|
|
||||||
|
|
||||||
expect(requestUrl.searchParams.get("fields")).toBe(
|
|
||||||
"id,name,picture.type(large)",
|
|
||||||
);
|
|
||||||
expect(requestUrl.searchParams.get("fields")).not.toContain("email");
|
|
||||||
expect(requestUrl.searchParams.get("access_token")).toBe("access-token");
|
|
||||||
expect(result).toEqual({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
id: "facebook-1",
|
|
||||||
name: "Elio",
|
|
||||||
pictureUrl: "https://example.com/avatar.jpg",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (result.success) expect(result.data).not.toHaveProperty("email");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -3,16 +3,15 @@
|
|||||||
*
|
*
|
||||||
* 在 OAuth callback 之后、auth state machine 的 `syncingFacebookBackend` actor
|
* 在 OAuth callback 之后、auth state machine 的 `syncingFacebookBackend` actor
|
||||||
* 成功换取业务 token 之后调用 —— 用 accessToken 调 `/me` 拉用户资料(id / name
|
* 成功换取业务 token 之后调用 —— 用 accessToken 调 `/me` 拉用户资料(id / name
|
||||||
* / picture),把 pictureUrl 写入 `UserStorage.setAvatarUrl()`、
|
* / email / picture),把 pictureUrl 写入 `UserStorage.setAvatarUrl()`、
|
||||||
* id 写入 `AuthStorage.setAsid()`。
|
* id 写入 `AuthStorage.setAsid()`。
|
||||||
*
|
*
|
||||||
* 端点:`https://graph.facebook.com/me?fields=id,name,picture.type(large)&access_token=...`
|
* 端点:`https://graph.facebook.com/me?fields=id,name,email,picture.type(large)&access_token=...`
|
||||||
* - `picture.type(large)` 返回 ~200px 头像 URL(默认 square 50px 太小)
|
* - `picture.type(large)` 返回 ~200px 头像 URL(默认 square 50px 太小)
|
||||||
* - 不指定 version 走最新稳定版
|
* - 不指定 version 走最新稳定版
|
||||||
* - 不需要 appId / appSecret(accessToken 自身就是凭证)
|
* - 不需要 appId / appSecret(accessToken 自身就是凭证)
|
||||||
*/
|
*/
|
||||||
import { FacebookUserData, FacebookUserDataSchema } from "@/data/schemas/auth";
|
import { FacebookUserData, FacebookUserDataSchema } from "@/data/schemas/auth";
|
||||||
import { FACEBOOK_PROFILE_FIELDS } from "@/lib/auth/facebook_provider";
|
|
||||||
import { Logger } from "@/utils/logger";
|
import { Logger } from "@/utils/logger";
|
||||||
import { Result, toError } from "@/utils/result";
|
import { Result, toError } from "@/utils/result";
|
||||||
|
|
||||||
@@ -23,9 +22,10 @@ const GRAPH_ME_URL = "https://graph.facebook.com/me";
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 拉取字段集合:
|
* 拉取字段集合:
|
||||||
* - id / name —— 给本地 FacebookUserData 数据类
|
* - id / name / email —— 给本地 FacebookUserData 数据类
|
||||||
* - picture.type(large) —— 头像 URL(默认 square 50px 太小)
|
* - picture.type(large) —— 头像 URL(默认 square 50px 太小)
|
||||||
*/
|
*/
|
||||||
|
const FIELDS = "id,name,email,picture.type(large)";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用 accessToken 调 Facebook Graph `/me` 拉用户资料。
|
* 用 accessToken 调 Facebook Graph `/me` 拉用户资料。
|
||||||
@@ -41,7 +41,7 @@ export async function fetchFacebookUserData(
|
|||||||
return Result.err(new Error("fetchFacebookUserData: accessToken is empty"));
|
return Result.err(new Error("fetchFacebookUserData: accessToken is empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = `${GRAPH_ME_URL}?fields=${encodeURIComponent(FACEBOOK_PROFILE_FIELDS)}&access_token=${encodeURIComponent(accessToken)}`;
|
const url = `${GRAPH_ME_URL}?fields=${encodeURIComponent(FIELDS)}&access_token=${encodeURIComponent(accessToken)}`;
|
||||||
|
|
||||||
log.debug(
|
log.debug(
|
||||||
{ url: url.replace(accessToken, "<redacted>") },
|
{ url: url.replace(accessToken, "<redacted>") },
|
||||||
@@ -72,6 +72,7 @@ export async function fetchFacebookUserData(
|
|||||||
const data = FacebookUserDataSchema.parse({
|
const data = FacebookUserDataSchema.parse({
|
||||||
id: typeof json.id === "string" ? json.id : null,
|
id: typeof json.id === "string" ? json.id : null,
|
||||||
name: typeof json.name === "string" ? json.name : null,
|
name: typeof json.name === "string" ? json.name : null,
|
||||||
|
email: typeof json.email === "string" ? json.email : null,
|
||||||
pictureUrl,
|
pictureUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,6 +80,7 @@ export async function fetchFacebookUserData(
|
|||||||
{
|
{
|
||||||
hasId: !!data.id,
|
hasId: !!data.id,
|
||||||
hasName: !!data.name,
|
hasName: !!data.name,
|
||||||
|
hasEmail: !!data.email,
|
||||||
hasPicture: !!data.pictureUrl,
|
hasPicture: !!data.pictureUrl,
|
||||||
},
|
},
|
||||||
"fetchFacebookUserData SUCCESS",
|
"fetchFacebookUserData SUCCESS",
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
const signInMock = vi.hoisted(() => vi.fn());
|
|
||||||
|
|
||||||
vi.mock("next-auth/react", () => ({
|
|
||||||
signIn: signInMock,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import { AuthPlatform } from "../auth_platform";
|
|
||||||
|
|
||||||
describe("AuthPlatform", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
signInMock.mockReset();
|
|
||||||
signInMock.mockResolvedValue(undefined);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("requests only public profile for Facebook login", async () => {
|
|
||||||
await AuthPlatform.facebookSignIn();
|
|
||||||
|
|
||||||
expect(signInMock).toHaveBeenCalledWith("facebook", undefined, {
|
|
||||||
scope: "public_profile",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("keeps Google login unchanged", async () => {
|
|
||||||
await AuthPlatform.googleSignIn();
|
|
||||||
|
|
||||||
expect(signInMock).toHaveBeenCalledWith("google");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
|
||||||
|
|
||||||
import {
|
|
||||||
createFacebookProvider,
|
|
||||||
FACEBOOK_AUTHORIZATION_SCOPE,
|
|
||||||
FACEBOOK_PROFILE_FIELDS,
|
|
||||||
mapFacebookProfile,
|
|
||||||
} from "../facebook_provider";
|
|
||||||
|
|
||||||
describe("Facebook provider", () => {
|
|
||||||
it("requests public profile without email permission or field", () => {
|
|
||||||
const provider = createFacebookProvider();
|
|
||||||
|
|
||||||
expect(provider.options).toMatchObject({
|
|
||||||
authorization: {
|
|
||||||
params: { scope: "public_profile" },
|
|
||||||
},
|
|
||||||
userinfo: {
|
|
||||||
params: { fields: "id,name,picture.type(large)" },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(FACEBOOK_AUTHORIZATION_SCOPE).not.toContain("email");
|
|
||||||
expect(FACEBOOK_PROFILE_FIELDS.split(",")).not.toContain("email");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not map an email even when Facebook unexpectedly returns one", () => {
|
|
||||||
const mapped = mapFacebookProfile({
|
|
||||||
id: "facebook-1",
|
|
||||||
name: "Elio",
|
|
||||||
email: "should-not-be-used@example.com",
|
|
||||||
picture: { data: { url: "https://example.com/avatar.jpg" } },
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(mapped).toEqual({
|
|
||||||
id: "facebook-1",
|
|
||||||
name: "Elio",
|
|
||||||
email: null,
|
|
||||||
image: "https://example.com/avatar.jpg",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -8,8 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
import NextAuth from "next-auth";
|
import NextAuth from "next-auth";
|
||||||
import Google from "next-auth/providers/google";
|
import Google from "next-auth/providers/google";
|
||||||
|
import Facebook from "next-auth/providers/facebook";
|
||||||
import { createFacebookProvider } from "./facebook_provider";
|
|
||||||
|
|
||||||
export const handler = NextAuth({
|
export const handler = NextAuth({
|
||||||
providers: [
|
providers: [
|
||||||
@@ -17,7 +16,10 @@ export const handler = NextAuth({
|
|||||||
clientId: process.env.AUTH_GOOGLE_ID ?? "",
|
clientId: process.env.AUTH_GOOGLE_ID ?? "",
|
||||||
clientSecret: process.env.AUTH_GOOGLE_SECRET ?? "",
|
clientSecret: process.env.AUTH_GOOGLE_SECRET ?? "",
|
||||||
}),
|
}),
|
||||||
createFacebookProvider(),
|
Facebook({
|
||||||
|
clientId: process.env.AUTH_FACEBOOK_ID ?? "",
|
||||||
|
clientSecret: process.env.AUTH_FACEBOOK_SECRET ?? "",
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
// 把 OAuth provider 的 token 塞进 NextAuth session,供前端取用:
|
// 把 OAuth provider 的 token 塞进 NextAuth session,供前端取用:
|
||||||
// - Google: account.id_token → 调后端 /api/auth/google-login 换业务 token
|
// - Google: account.id_token → 调后端 /api/auth/google-login 换业务 token
|
||||||
|
|||||||
@@ -17,8 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
|
|
||||||
import { FACEBOOK_AUTHORIZATION_SCOPE } from "./facebook_provider";
|
|
||||||
|
|
||||||
export type AuthProvider = "facebook" | "google";
|
export type AuthProvider = "facebook" | "google";
|
||||||
|
|
||||||
export class AuthPlatform {
|
export class AuthPlatform {
|
||||||
@@ -29,8 +27,6 @@ export class AuthPlatform {
|
|||||||
|
|
||||||
/** 触发 Facebook OAuth 跳转 */
|
/** 触发 Facebook OAuth 跳转 */
|
||||||
static async facebookSignIn(): Promise<void> {
|
static async facebookSignIn(): Promise<void> {
|
||||||
await signIn("facebook", undefined, {
|
await signIn("facebook");
|
||||||
scope: FACEBOOK_AUTHORIZATION_SCOPE,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
import Facebook, {
|
|
||||||
type FacebookProfile,
|
|
||||||
} from "next-auth/providers/facebook";
|
|
||||||
|
|
||||||
export const FACEBOOK_AUTHORIZATION_SCOPE = "public_profile";
|
|
||||||
export const FACEBOOK_PROFILE_FIELDS = "id,name,picture.type(large)";
|
|
||||||
|
|
||||||
export function createFacebookProvider() {
|
|
||||||
return Facebook({
|
|
||||||
clientId: process.env.AUTH_FACEBOOK_ID ?? "",
|
|
||||||
clientSecret: process.env.AUTH_FACEBOOK_SECRET ?? "",
|
|
||||||
authorization: {
|
|
||||||
params: { scope: FACEBOOK_AUTHORIZATION_SCOPE },
|
|
||||||
},
|
|
||||||
userinfo: {
|
|
||||||
params: { fields: FACEBOOK_PROFILE_FIELDS },
|
|
||||||
},
|
|
||||||
profile: mapFacebookProfile,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function mapFacebookProfile(profile: FacebookProfile) {
|
|
||||||
return {
|
|
||||||
id: profile.id,
|
|
||||||
name: typeof profile.name === "string" ? profile.name : null,
|
|
||||||
email: null,
|
|
||||||
image:
|
|
||||||
typeof profile.picture?.data?.url === "string"
|
|
||||||
? profile.picture.data.url
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user