Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Server token #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/api/auth/[...nextauth]/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ export const {

return token;
},
async session({ session, token }) {
session.accessToken = token.accessToken;
session.user = token.user;
return session;
},
},
// session: {
// strategy: 'jwt', // <-- make sure to use jwt here
// maxAge: 30 * 24 * 60 * 60,
// },
secret:
process.env.NEXTAUTH_SECRET ??
(function () {
Expand Down
13 changes: 13 additions & 0 deletions app/api/foo/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextApiRequest, NextApiResponse } from "next";
import { auth } from "../auth/[...nextauth]/auth";

export async function GET(req: NextApiRequest, res: NextApiResponse) {
const session = await auth(req, new Response() as any);
console.log('🔥');
console.log(session);
console.log(JSON.stringify(session));
console.log(session?.user);
console.log(session?.accessToken);
console.log('🔥');
return new Response('OK', { status: 200 });
}
3 changes: 3 additions & 0 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import NewPost from '@/components/new-post';
import Post from '@/components/post';
import { getPostList } from '@/mumble/api';
import { auth } from './api/auth/[...nextauth]/auth';
import { NextRequest, NextResponse } from 'next/server';
import FooPost from '@/components/foo-post';

export default async function Home() {
const session = await auth();
Expand All @@ -31,6 +33,7 @@ export default async function Home() {
</div>
</div>
)}
<FooPost />
{session && (
<div>
<h2>Create a post</h2>
Expand Down
12 changes: 12 additions & 0 deletions components/foo-post.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client';

export default function FooPost() {
return (
<button
className="bg-red-400 text-white font-bold py-2 px-4 rounded"
onClick={() => fetch('/api/foo')}
>
Foo it
</button>);

}
2 changes: 1 addition & 1 deletion components/live-posts.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';

import { PostEvents, getPostEventSource } from '@/mumble/api';
import { Post as ApiPost } from '@/mumble/types';
import { useEffect, useState } from 'react';
import Post from './post';
import { PostEvents, getPostEventSource } from '@/mumble/foo';

export default function LivePosts() {
const [posts, setPosts] = useState<ApiPost[]>([]);
Expand Down
1 change: 1 addition & 0 deletions components/new-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function NewPost() {
return;
}


const text = data.get('text') as string;
await createPost(text);
}
Expand Down
29 changes: 14 additions & 15 deletions mumble/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use server';

import { auth } from '@/app/api/auth/[...nextauth]/auth';
import { PaginatedResult, Post } from './types';
import { cookies, headers } from 'next/headers';

const apiUrl = 'https://mumble-api-prod-4cxdci3drq-oa.a.run.app';

Expand All @@ -8,11 +11,19 @@ function authHeader(accessToken?: string): HeadersInit {
}

export async function getPostList() {
const session = await auth();

const req = {
headers: Object.fromEntries(headers() as Headers),
cookies: Object.fromEntries(
cookies()
.getAll()
.map((c) => [c.name, c.value])
),
};

const token = await auth(req as any);
const res = await fetch(`${apiUrl}/posts`, {
headers: {
...authHeader(session?.accessToken),
...authHeader(token?.accessToken),
},
});
const posts = (await res.json()) as PaginatedResult<Post>;
Expand All @@ -35,15 +46,3 @@ export async function createPost(text: string) {
const post = (await res.json()) as Post;
return post;
}

export enum PostEvents {
created = 'postCreated',
updated = 'postUpdated',
deleted = 'postDeleted',
liked = 'postLiked',
unliked = 'postUnliked',
}

export function getPostEventSource() {
return new EventSource(`${apiUrl}/posts/_sse`);
}
14 changes: 14 additions & 0 deletions mumble/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const apiUrl = 'https://mumble-api-prod-4cxdci3drq-oa.a.run.app';

export enum PostEvents {
created = 'postCreated',
updated = 'postUpdated',
deleted = 'postDeleted',
liked = 'postLiked',
unliked = 'postUnliked',
}

export function getPostEventSource() {
return new EventSource(`${apiUrl}/posts/_sse`);
}