Skip to content

Commit

Permalink
Merge pull request #13 from farcasterxyz/horsefacts/next-auth
Browse files Browse the repository at this point in the history
feat: simple profile component
  • Loading branch information
nickcherry authored Jan 26, 2024
2 parents d49f578 + 10c8265 commit 520b533
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
26 changes: 13 additions & 13 deletions web/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { createAppClient, viemConnector } from "@farcaster/auth-client";
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

const auth = NextAuth({
import { getProfile } from '../../../../lib/services/user';

export const authOptions = {
providers: [
CredentialsProvider({
name: "Sign in with Farcaster",
Expand All @@ -20,15 +22,6 @@ const auth = NextAuth({
csrfToken: {
type: "text",
},
// In a production app with a server, these should be fetched from
// your Farcaster data indexer rather than have them accepted as part
// of credentials.
name: {
type: "text",
},
pfp: {
type: "text",
},
},
async authorize(credentials) {
const appClient = createAppClient({
Expand All @@ -50,13 +43,20 @@ const auth = NextAuth({

return {
id: fid.toString(),
name: credentials?.name,
image: credentials?.pfp,
};
},
}),
],
});
callbacks: {
async session({ session, token }) {
const profile = await getProfile({ fid: token.sub });
session.user = profile;
return session;
},
},
};

const auth = NextAuth(authOptions);

export const GET = auth;
export const POST = auth;
22 changes: 15 additions & 7 deletions web/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// import { getServerSession } from 'next-auth';
import { FeedPage } from '@components/feed/FeedPage';
import { LandingPage } from '@components/landing/LandingPage';
import { getServerSession } from 'next-auth';

import { FeedPage } from "@components/feed/FeedPage";
import { LandingPage } from "@components/landing/LandingPage";
import { authOptions } from './api/auth/[...nextauth]/route';

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

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

return <LandingPage />;
}
4 changes: 2 additions & 2 deletions web/src/components/avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import { User } from '@shared/types/models';
import { useState } from 'react';

const pfpDiameter = 46;
const fallbackUrl = '/default-avatar.png';

type AvatarProps = {
user: User;
pfpDiameter?: number;
};

export function Avatar({ user }: AvatarProps) {
export function Avatar({ user, pfpDiameter = 46 }: AvatarProps) {
const [src, setSrc] = useState(user.pfp_url || fallbackUrl);

return (
Expand Down
5 changes: 5 additions & 0 deletions web/src/components/feed/FeedPage.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Cast } from "@components/feed/Cast";
import { Profile } from '@components/profile/Profile';
import { getFeed } from "@lib/services/feed";

import { getProfile } from '../../lib/services/user';

type FeedPageProps = {
fid: string;
};

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

return (
<div className="container m-auto max-w-[660px] border-x border-gray-200">
<Profile user={user} />
{feed.map((cast) => (
<Cast key={cast.hash} cast={cast} />
))}
Expand Down
22 changes: 22 additions & 0 deletions web/src/components/profile/Profile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { User } from '@shared/types/models';

import { Avatar } from '../avatar/Avatar';

type ProfileProps = {
user: User;
};

export function Profile({ user }: ProfileProps) {
return (
<div className="flex flex-col p-4">
<div className="flex flex-row place-items-center">
<Avatar user={user} pfpDiameter={72} />
<div className="flex flex-col ml-4 text-l">
<span className="font-bold">{user.display_name}</span>
<span className="text-gray-500">@{user.username}</span>
<div className="mt-2">{user.bio}</div>
</div>
</div>
</div>
);
}

0 comments on commit 520b533

Please sign in to comment.