Skip to content

Commit

Permalink
web auth working
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcherry committed Jan 30, 2024
1 parent 488775d commit 9849a46
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 34 deletions.
18 changes: 10 additions & 8 deletions web/src/app/api/auth/sign-in/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createAppClient, viemConnector } from '@farcaster/auth-kit';
import { createAppClient, viemConnector } from '@farcaster/auth-client';
import { setCurrentUser } from '@lib/auth/setCurrentUser';
import { randomUUID } from 'crypto';
import { NextResponse } from 'next/server';

type RequestBody = {
Expand All @@ -21,16 +23,16 @@ export async function POST(request: Request) {
signature,
});

if (!verifyResult.success) {
return NextResponse.json(verifyResult.error || 'Sign in failed', {
if (verifyResult.isError) {
return NextResponse.json(verifyResult.error?.message || 'Sign in failed', {
status: 401,
});
}

const token = window.crypto.randomUUID();
const token = randomUUID();
const fid = verifyResult.fid.toString();

return NextResponse.json({
id: verifyResult.fid.toString(),
token,
});
setCurrentUser({ token, fid });

return NextResponse.json({ fid, token });
}
7 changes: 7 additions & 0 deletions web/src/app/api/auth/sign-out/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { clearCurrentUser } from '@lib/auth/clearCurrentUser';
import { NextResponse } from 'next/server';

export async function POST() {
clearCurrentUser();
return NextResponse.json({ success: true });
}
21 changes: 11 additions & 10 deletions web/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { FeedPage } from '@components/feed/FeedPage';
import { LandingPage } from '@components/landing/LandingPage';
import { getCurrentUser } from '@lib/auth/getCurrentUser';

export default async function Home() {
// if (session) {
// const {
// user: { fid },
// } = session;
// return (
// <div>
// <FeedPage fid={fid} />
// </div>
// );
// }
const user = await getCurrentUser();

if (user) {
return (
<div>
<FeedPage user={user} />
</div>
);
}

return <LandingPage />;
}
17 changes: 8 additions & 9 deletions web/src/components/feed/FeedPage.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Feed } from "@components/feed/Feed";
import { Nav } from "@components/nav/Nav";
import { Profile } from "@components/profile/Profile";
import { getFeed } from "@lib/services/feed";
import { getProfile } from "@lib/services/user";
import { Feed } from '@components/feed/Feed';
import { Nav } from '@components/nav/Nav';
import { Profile } from '@components/profile/Profile';
import { getFeed } from '@lib/services/feed';
import { User } from '@shared/types/models';

type FeedPageProps = {
fid: string;
user: User;
};

export async function FeedPage({ fid }: FeedPageProps) {
const feed = await getFeed({ fid });
const user = await getProfile({ fid });
export async function FeedPage({ user }: FeedPageProps) {
const feed = await getFeed({ fid: user.fid });

return (
<div className="container m-auto max-w-[660px] border-x border-gray-200">
Expand Down
11 changes: 7 additions & 4 deletions web/src/components/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import {
import { useState } from 'react';

async function handleSuccess(res: StatusAPIResponse) {
await fetch('/api/auth/sign-in', {
const signInresponse = await fetch('/api/auth/sign-in', {
method: 'POST',
body: JSON.stringify({
message: res.message,
nonce: res.nonce,
signature: res.signature,
}),
});

window.location.reload();
if (signInresponse.ok) {
window.location.reload();
} else {
alert('Sign in failed');
}
}

export default function Login() {
Expand All @@ -39,5 +44,3 @@ export default function Login() {
</AuthKitProvider>
);
}

function Button() {}
7 changes: 4 additions & 3 deletions web/src/components/logout/Logout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use client';

import { signOut } from 'next-auth/react';

export default function Logout() {
return (
<button
type="button"
className="bg-fc-purple cursor-pointer rounded px-4 py-2 text-white"
onClick={() => signOut()}
onClick={async () => {
await fetch('/api/auth/sign-out');
window.location.reload();
}}
>
Sign out
</button>
Expand Down
14 changes: 14 additions & 0 deletions web/src/lib/auth/clearCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { cookies } from 'next/headers';

import { getTokenFromCookieOrHeader } from './getTokenFromCookieOrHeader';
import { tokenKey, tokens } from './shared';

export function clearCurrentUser() {
const token = getTokenFromCookieOrHeader();

cookies().delete(tokenKey);

if (token) {
delete tokens[token];
}
}
21 changes: 21 additions & 0 deletions web/src/lib/auth/getCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getProfile } from '@lib/services/user';
import { User } from '@shared/types/models';

import { getTokenFromCookieOrHeader } from './getTokenFromCookieOrHeader';
import { tokens } from './shared';

export async function getCurrentUser(): Promise<User | null> {
const token = getTokenFromCookieOrHeader();

if (!token) {
return null;
}

const fid = tokens[token];

if (!fid) {
return null;
}

return getProfile({ fid });
}
7 changes: 7 additions & 0 deletions web/src/lib/auth/getTokenFromCookieOrHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { cookies, headers } from 'next/headers';

import { tokenKey } from './shared';

export function getTokenFromCookieOrHeader(): string | null {
return cookies().get(tokenKey)?.value || headers().get(tokenKey);
}
8 changes: 8 additions & 0 deletions web/src/lib/auth/setCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { cookies } from 'next/headers';

import { tokenKey, tokens } from './shared';

export function setCurrentUser({ token, fid }: { token: string; fid: string }) {
cookies().set(tokenKey, token, { secure: true });
tokens[token] = fid;
}
2 changes: 2 additions & 0 deletions web/src/lib/auth/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const tokenKey = 'token';
export const tokens: Record<string, string> = {};

0 comments on commit 9849a46

Please sign in to comment.