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(nextjs-appwrite): auth flow #396

Merged
Merged
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
10 changes: 5 additions & 5 deletions refine-nextjs/plugins/data-provider-appwrite/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const base = {
_app: {
import: [],
localImport: [
'import { authProvider } from "@providers/auth-provider";',
'import { dataProvider, liveProvider } from "@providers/data-provider";',
'import { authProviderClient } from "@providers/auth-provider";',
'import { appwriteDataProvider, appwriteLiveProvider } from "@providers/data-provider";',
],
refineProps: [
`dataProvider={dataProvider}`,
`liveProvider={liveProvider}`,
`authProvider={authProvider}`,
"dataProvider={appwriteDataProvider}",
"liveProvider={appwriteLiveProvider}",
"authProvider={authProviderClient}",
],
refineOptions: [`liveMode: "auto",`],
refineAntdImports: [],
Expand Down
20 changes: 10 additions & 10 deletions refine-nextjs/plugins/data-provider-appwrite/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"dependencies": {
"@refinedev/appwrite": "^6.4.6",
"uuid": "^9.0.0",
"js-cookie": "^3.0.5"
},
"devDependencies": {
"@types/uuid": "^9.0.2",
"@types/js-cookie": "^3.0.6"
}
"dependencies": {
"@refinedev/appwrite": "^6.4.6",
"uuid": "^9.0.0",
"js-cookie": "^3.0.5",
"node-appwrite": "^13.0.0"
},
"devDependencies": {
"@types/uuid": "^9.0.2",
"@types/js-cookie": "^3.0.6"
}
}

Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
"use client";

import { account, appwriteClient } from "@providers/data-provider";
import { AppwriteException } from "@refinedev/appwrite";
import { AuthBindings } from "@refinedev/core";
import { APPWRITE_TOKEN_KEY } from "@utility/constants";
import { AuthProvider } from "@refinedev/core";
import { appwriteAccount, appwriteClient } from "@utils/appwrite/client";
import { APPWRITE_JWT_KEY } from "@utils/constants";
import Cookies from "js-cookie";
import { v4 as uuidv4 } from "uuid";

export const authProvider: AuthBindings = {
export const authProviderClient: AuthProvider = {
login: async ({ email, password }) => {
try {
await account.createEmailSession(email, password);
const { jwt } = await account.createJWT();
Cookies.remove(APPWRITE_JWT_KEY, { path: "/" });
appwriteClient.setJWT("");

await appwriteAccount.createEmailSession(email, password);
const { jwt } = await appwriteAccount.createJWT();
appwriteClient.setJWT(jwt);

if (jwt) {
Cookies.set(APPWRITE_TOKEN_KEY, jwt, {
Cookies.set(APPWRITE_JWT_KEY, jwt, {
expires: 30, // 30 days
path: "/",
});
Expand All @@ -37,14 +41,10 @@ export const authProvider: AuthBindings = {
},
logout: async () => {
try {
await account.deleteSession("current");
} catch (error: any) {
return {
success: false,
error,
};
}
Cookies.remove(APPWRITE_TOKEN_KEY, { path: "/" });
await appwriteAccount.deleteSessions();
} catch (error) {}

Cookies.remove(APPWRITE_JWT_KEY, { path: "/" });
appwriteClient.setJWT("");
return {
success: true,
Expand All @@ -53,7 +53,7 @@ export const authProvider: AuthBindings = {
},
register: async ({ email, password }) => {
try {
await account.create(uuidv4(), email, password);
await appwriteAccount.create(uuidv4(), email, password);
return {
success: true,
redirectTo: "/login",
Expand All @@ -69,18 +69,20 @@ export const authProvider: AuthBindings = {
};
}
},
onError: async (error) => {
console.error(error);
onError: async (error: AppwriteException) => {
if (error.code === 401) {
return { logout: true, redirectTo: "/login", error };
}
return { error };
},
check: async () => {
const appwriteJWT = Cookies.get(APPWRITE_TOKEN_KEY);
const appwriteJWT = Cookies.get(APPWRITE_JWT_KEY);
if (appwriteJWT) {
appwriteClient.setJWT(appwriteJWT);
}

try {
const session = await account.get();
const session = await appwriteAccount.get();

if (session) {
return {
Expand Down Expand Up @@ -108,7 +110,7 @@ export const authProvider: AuthBindings = {
},
getPermissions: async () => null,
getIdentity: async () => {
const user = await account.get();
const user = await appwriteAccount.get();

if (user) {
return user;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { AuthBindings } from "@refinedev/core";
import { APPWRITE_TOKEN_KEY } from "@utility/constants";
import { cookies } from "next/headers";
import { AuthProvider } from "@refinedev/core";
import { getSessionClient } from "@utils/appwrite/server";

export const authProviderServer: Pick<AuthBindings, "check"> = {
export const authProviderServer: Pick<AuthProvider, "check"> = {
check: async () => {
const cookieStore = cookies();
const auth = cookieStore.get(APPWRITE_TOKEN_KEY);
try {
const client = await getSessionClient();
await client.account.get();

if (auth) {
return {
authenticated: true,
};
} catch (error: any) {
return {
authenticated: false,
logout: true,
redirectTo: "/login",
error,
};
}

return {
authenticated: false,
logout: true,
redirectTo: "/login",
};
},
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './auth-provider'
export * from './auth-provider.client'
export * from './auth-provider.server'
Original file line number Diff line number Diff line change
@@ -1,38 +1,12 @@
"use client";

import {
Account,
Appwrite,
dataProvider as appwriteDataProvider,
liveProvider as appwriteLiveProvider,
Storage,
} from "@refinedev/appwrite";
import {
APPWRITE_PROJECT,
APPWRITE_TOKEN_KEY,
APPWRITE_URL,
} from "@utility/constants";
import Cookies from "js-cookie";
import { dataProvider, liveProvider } from "@refinedev/appwrite";
import { appwriteClient } from "@utils/appwrite/client";

const appwriteClient = new Appwrite();

appwriteClient.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT);

// for client side authentication
const appwriteJWT = Cookies.get(APPWRITE_TOKEN_KEY);
if (appwriteJWT) {
appwriteClient.setJWT(appwriteJWT);
}

const account = new Account(appwriteClient);
const storage = new Storage(appwriteClient);

export { appwriteClient, account, storage };

export const dataProvider = appwriteDataProvider(appwriteClient, {
export const appwriteDataProvider = dataProvider(appwriteClient, {
databaseId: "database",
});

export const liveProvider = appwriteLiveProvider(appwriteClient, {
export const appwriteLiveProvider = liveProvider(appwriteClient, {
databaseId: "database",
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use client'

import { Account, Appwrite, Storage } from '@refinedev/appwrite'
import { APPWRITE_JWT_KEY, APPWRITE_PROJECT, APPWRITE_URL } from '@utils/constants'
import Cookies from 'js-cookie'

export const appwriteClient = new Appwrite()

const appwriteJWT = Cookies.get(APPWRITE_JWT_KEY)
if (appwriteJWT) {
appwriteClient.setJWT(appwriteJWT)
}

appwriteClient.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT)

export const appwriteAccount = new Account(appwriteClient)
export const appwriteStorage = new Storage(appwriteClient)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Client, Account } from 'node-appwrite'
import { cookies } from 'next/headers'
import { APPWRITE_JWT_KEY, APPWRITE_PROJECT, APPWRITE_URL } from '@utils/constants'

export const getSessionClient = async () => {
const client = new Client().setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT)

const session = cookies().get(APPWRITE_JWT_KEY)
if (!session || !session.value) {
throw new Error('No session')
}

client.setJWT(session.value)

return {
get account() {
return new Account(client)
},
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const APPWRITE_URL = "https://refine.appwrite.org/v1";
export const APPWRITE_PROJECT = "61c4368b4e349";
export const APPWRITE_TOKEN_KEY = "appwrite-jwt";
export const APPWRITE_JWT_KEY = "appwrite-jwt";
2 changes: 1 addition & 1 deletion refine-nextjs/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ module.exports = {
when: function (answers) {
return answers["ui-framework"] !== "antd";
},
pattern: ["src/utility/normalize.ts"],
pattern: ["src/utils/normalize.ts"],
},
{
plugin: ["_base"],
Expand Down
Loading