Skip to content

Commit

Permalink
fix: nextjs auth
Browse files Browse the repository at this point in the history
  • Loading branch information
alicanerdurmaz committed Jan 18, 2024
1 parent 20059b2 commit 9696b98
Show file tree
Hide file tree
Showing 25 changed files with 1,341 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "ejslint ./refine-*/**/*",
"prepare": "husky install",
"cypress": "cypress open --env=AUTH_PROVIDER=custom,UI_FRAMEWORK=mui,FRAMEWORK=react,DATA_PROVIDER=custom-json-rest"
"cypress": "cypress open --env=AUTH_PROVIDER=custom,UI_FRAMEWORK=no,FRAMEWORK=nextjs,DATA_PROVIDER=airtable"
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { Create, useForm, useSelect } from "@refinedev/antd";
import { IResourceComponentsProps } from "@refinedev/core";
import { Form, Input, Select } from "antd";
import React from "react";
import { GetServerSideProps } from "next";
<%_ if (_app.isAuthProviderCheck) { _%>
import { authProvider } from "src/authProvider";
<%_ } _%>
<%_ if (_app.isNextAuthCheck) { _%>
import { getServerSession } from "next-auth";
import { authOptions } from "../api/auth/[...nextauth]";
<%_ } _%>
<%_ if (answers["data-provider"] === "data-provider-hasura") { _%>
import { BLOG_POSTS_QUERY, BLOG_POSTS_CATEGORIES_SELECT_QUERY } from "../../../src/queries/blog-posts";
<%_ } _%>
Expand Down Expand Up @@ -88,3 +96,51 @@ export default function BlogPostCreate() {
</Create>
);
};


export const getServerSideProps: GetServerSideProps<{}> = async (context) => {
<%_ if (_app.isNextAuthCheck) { _%>
const session = await getServerSession(
context.req,
context.res,
authOptions,
);
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
const { authenticated, redirectTo } = await authProvider.check(context);
<%_ } _%>

<%_ if (_app.isNextAuthCheck) { _%>
if (!session) {
return {
props: {
},
redirect: {
destination: `/login?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
if (!authenticated) {
return {
props: {

},
redirect: {
destination: `${redirectTo}?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

return {
props: {

},
};
};
56 changes: 56 additions & 0 deletions refine-nextjs/plugins/antd-example/pages/blog-posts/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { Edit, useForm, useSelect } from "@refinedev/antd";
import { IResourceComponentsProps } from "@refinedev/core";
import { Form, Input, Select } from "antd";
import React from "react";
import { GetServerSideProps } from "next";
<%_ if (_app.isAuthProviderCheck) { _%>
import { authProvider } from "src/authProvider";
<%_ } _%>
<%_ if (_app.isNextAuthCheck) { _%>
import { getServerSession } from "next-auth";
import { authOptions } from "../api/auth/[...nextauth]";
<%_ } _%>
<%_ if (answers["data-provider"] === "data-provider-hasura") { _%>
import { BLOG_POSTS_QUERY, BLOG_POSTS_CATEGORIES_SELECT_QUERY } from "../../../src/queries/blog-posts";
<%_ } _%>
Expand Down Expand Up @@ -96,3 +104,51 @@ export default function BlogPostEdit() {
</Edit>
);
};


export const getServerSideProps: GetServerSideProps<{}> = async (context) => {
<%_ if (_app.isNextAuthCheck) { _%>
const session = await getServerSession(
context.req,
context.res,
authOptions,
);
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
const { authenticated, redirectTo } = await authProvider.check(context);
<%_ } _%>

<%_ if (_app.isNextAuthCheck) { _%>
if (!session) {
return {
props: {
},
redirect: {
destination: `/login?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
if (!authenticated) {
return {
props: {

},
redirect: {
destination: `${redirectTo}?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

return {
props: {

},
};
};
56 changes: 56 additions & 0 deletions refine-nextjs/plugins/antd-example/pages/blog-posts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import {
import { BaseRecord, IResourceComponentsProps, useMany } from "@refinedev/core";
import { Space, Table } from "antd";
import React from "react";
import { GetServerSideProps } from "next";
<%_ if (_app.isAuthProviderCheck) { _%>
import { authProvider } from "src/authProvider";
<%_ } _%>
<%_ if (_app.isNextAuthCheck) { _%>
import { getServerSession } from "next-auth";
import { authOptions } from "../api/auth/[...nextauth]";
<%_ } _%>

<%_ if (answers["data-provider"] === "data-provider-hasura") { _%>
import { BLOG_POSTS_QUERY, BLOG_POSTS_CATEGORIES_SELECT_QUERY } from "../../src/queries/blog-posts";
<%_ } _%>
Expand Down Expand Up @@ -105,3 +114,50 @@ export default function BlogPostList() {
</List>
);
};

export const getServerSideProps: GetServerSideProps<{}> = async (context) => {
<%_ if (_app.isNextAuthCheck) { _%>
const session = await getServerSession(
context.req,
context.res,
authOptions,
);
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
const { authenticated, redirectTo } = await authProvider.check(context);
<%_ } _%>

<%_ if (_app.isNextAuthCheck) { _%>
if (!session) {
return {
props: {
},
redirect: {
destination: `/login?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
if (!authenticated) {
return {
props: {

},
redirect: {
destination: `${redirectTo}?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

return {
props: {

},
};
};
56 changes: 56 additions & 0 deletions refine-nextjs/plugins/antd-example/pages/blog-posts/show/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import {
import { IResourceComponentsProps, useOne, useShow } from "@refinedev/core";
import { Typography } from "antd";
import React from "react";
import { GetServerSideProps } from "next";
<%_ if (_app.isAuthProviderCheck) { _%>
import { authProvider } from "src/authProvider";
<%_ } _%>
<%_ if (_app.isNextAuthCheck) { _%>
import { getServerSession } from "next-auth";
import { authOptions } from "../api/auth/[...nextauth]";
<%_ } _%>
<%_ if (answers["data-provider"] === "data-provider-hasura") { _%>
import { BLOG_POSTS_QUERY, BLOG_POSTS_CATEGORIES_SELECT_QUERY } from "../../../src/queries/blog-posts";
<%_ } _%>
Expand Down Expand Up @@ -66,3 +74,51 @@ export default function BlogPostShow() {
</Show>
);
};


export const getServerSideProps: GetServerSideProps<{}> = async (context) => {
<%_ if (_app.isNextAuthCheck) { _%>
const session = await getServerSession(
context.req,
context.res,
authOptions,
);
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
const { authenticated, redirectTo } = await authProvider.check(context);
<%_ } _%>

<%_ if (_app.isNextAuthCheck) { _%>
if (!session) {
return {
props: {
},
redirect: {
destination: `/login?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
if (!authenticated) {
return {
props: {

},
redirect: {
destination: `${redirectTo}?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

return {
props: {

},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { Create, useForm } from "@refinedev/antd";
import { IResourceComponentsProps } from "@refinedev/core";
import { Form, Input } from "antd";
import React from "react";
import { GetServerSideProps } from "next";
<%_ if (_app.isAuthProviderCheck) { _%>
import { authProvider } from "src/authProvider";
<%_ } _%>
<%_ if (_app.isNextAuthCheck) { _%>
import { getServerSession } from "next-auth";
import { authOptions } from "../api/auth/[...nextauth]";
<%_ } _%>
<%_ if (answers["data-provider"] === "data-provider-hasura") { _%>
import { CATEGORIES_QUERY } from "../../../src/queries/categories";
<%_ } _%>
Expand Down Expand Up @@ -33,3 +41,51 @@ export default function CategoryCreate() {
</Create>
);
};


export const getServerSideProps: GetServerSideProps<{}> = async (context) => {
<%_ if (_app.isNextAuthCheck) { _%>
const session = await getServerSession(
context.req,
context.res,
authOptions,
);
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
const { authenticated, redirectTo } = await authProvider.check(context);
<%_ } _%>

<%_ if (_app.isNextAuthCheck) { _%>
if (!session) {
return {
props: {
},
redirect: {
destination: `/login?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

<%_ if (_app.isAuthProviderCheck) { _%>
if (!authenticated) {
return {
props: {

},
redirect: {
destination: `${redirectTo}?to=${encodeURIComponent("/blog-posts")}`,
permanent: false,
},
};
}
<%_ } _%>

return {
props: {

},
};
};
Loading

0 comments on commit 9696b98

Please sign in to comment.