Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Feature/app 25 Add Middleware
Browse files Browse the repository at this point in the history
Feature/app 25 Add Middleware
  • Loading branch information
CaroleHug authored Apr 24, 2023
2 parents 5e0011d + f5bbaf8 commit 8c6d92f
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 23 deletions.
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const withPWA = require('next-pwa')({
dest: 'public',
disable: process.env.NODE_ENV === 'development',
});

module.exports = withPWA({
Expand Down
2 changes: 1 addition & 1 deletion src/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const Header: FC = () => {
<SettingsIcon size={24} />
</IconButton>
</div>
<a href="#" onClick={() => signOut()}>
<a href="#" onClick={() => signOut({ redirect: true, callbackUrl: '/login' })}>
<IconButton label={'Logout'} variant={'purple'} iconText={'Logout'}>
<LogoutIcon size={24} />
</IconButton>
Expand Down
5 changes: 5 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default } from 'next-auth/middleware';

export const config = {
matcher: ['/', '/mumble/:id?', '/profile/:id?', '/tag/:id?'],
};
3 changes: 3 additions & 0 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export default NextAuth({
session: {
maxAge: 12 * 60 * 60, // 12 hours
},
pages: {
signIn: '/login',
},
callbacks: {
async jwt({ token, user, account }) {
if (account) {
Expand Down
11 changes: 1 addition & 10 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,9 @@ export default function PageHome({ mumbles, users, error, count }: InferGetServe
export const getServerSideProps: GetServerSideProps = async (context) => {
const session = await getSession(context);

if (!session) {
return {
redirect: {
destination: '/login',
permanent: false,
},
};
}

try {
const { count, mumbles } = await fetchMumbles({ limit: 10 });
const { users } = await fetchUsers({ accessToken: session.accessToken });
const { users } = await fetchUsers({ accessToken: session?.accessToken });

return { props: { count, mumbles, users } };
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Home() {
<div className={'flex justify-center items-center'}>
<div>
<h1 className={'label-XL'}>Anmelden</h1>
<Button label="Let's mumble" onClick={() => signIn('zitadel')} size="L" variant="gradient">
<Button label="Let's mumble" onClick={() => signIn('zitadel', { callbackUrl: '/' })} size="L" variant="gradient">
<MumbleIcon size={16} />
</Button>
</div>
Expand Down
11 changes: 0 additions & 11 deletions src/services/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ export const createPost = async (postArgs: PostArgs) => {
export const commentPost = async (params: { postId: string; comment: string; accessToken?: string }) => {
const { postId, comment, accessToken } = params || {};

if (!accessToken) {
throw new Error('No access token');
}

const res = await axios.post(
`${process.env.NEXT_PUBLIC_QWACKER_API_URL}posts/${postId}`,
{ text: comment },
Expand All @@ -87,10 +83,6 @@ export const commentPost = async (params: { postId: string; comment: string; acc
export const fetchMumbleById = async (params?: { postId: string; accessToken?: string }) => {
const { postId, accessToken } = params || {};

if (!accessToken) {
throw new Error('No access token');
}

const url = `${process.env.NEXT_PUBLIC_QWACKER_API_URL}posts/${postId}`;

const res = await axios.get(url, {
Expand All @@ -108,9 +100,6 @@ export const fetchMumbleById = async (params?: { postId: string; accessToken?: s

export const fetchReplies = async (params?: { postId: string; accessToken?: string }) => {
const { postId, accessToken } = params || {};
if (!accessToken) {
throw new Error('No access token');
}

const url = `${process.env.NEXT_PUBLIC_QWACKER_API_URL}posts/${postId}/replies`;

Expand Down

1 comment on commit 8c6d92f

@vercel
Copy link

@vercel vercel bot commented on 8c6d92f Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.