Amazon Cognito Auth Provider Support #3469
-
Is your feature request related to a problem? Please describe.It would be great if we set up an example for Amazon Cognito. Describe alternatives you've consideredNo response Additional contexthttps://refine.dev/docs/examples/auth-provider/auth0/ Describe the thing to improveWe can create a quick starting point for AWS Cognito users |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 2 replies
-
@omeraplak can you assign it to me and i will try to have a shot at it ?? |
Beta Was this translation helpful? Give feedback.
-
Hey @Smartmind12 , |
Beta Was this translation helpful? Give feedback.
-
Hey, yes i have worked on it during my hackathons so i have knowledge about it |
Beta Was this translation helpful? Give feedback.
-
Cool, Happy Hacking! 🚀 |
Beta Was this translation helpful? Give feedback.
-
Update: I have been busy in the last few days … so i will start working on it by today and submit! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hey @Smartmind12 , |
Beta Was this translation helpful? Give feedback.
-
I have run it on local and pushed some of the files in the branch and adding rest of the files…once thats done i will add the PR! |
Beta Was this translation helpful? Give feedback.
-
Due to inactivity unassigned @Smartmind12 from this issue. Anyone still interested in this? |
Beta Was this translation helpful? Give feedback.
-
For anyone looking for drop-in authProvider with amplify-js cognito integration: import { AuthBindings } from "@refinedev/core";
import { Amplify, Auth } from "aws-amplify";
import type { AuthActionResponse } from "@refinedev/core/dist/interfaces";
import { env } from "./env";
Amplify.configure({
Auth: {
// fixme: set your own ids.
userPoolId: env.VITE_USERPOOL_ID,
userPoolWebClientId: env.VITE_USERPOOL_WEB_CLIENT_ID,
},
});
interface CognitoUser {
username: string;
attributes: {
name: string;
picture?: string;
email: string;
// fixme: put your own permission attribute here if you have one.
// "custom:permissions"?: string;
};
}
const getErrorResponse = (error: unknown): AuthActionResponse => {
if (error instanceof Error) {
return {
success: false,
error: error,
};
}
if (typeof error === "string") {
return {
success: false,
error: new Error(error),
};
} else {
return {
success: false,
error: new Error("Unknown error"),
};
}
};
const authProvider: AuthBindings = {
login: async (input) => {
const { email, password } = input;
try {
const user = await Auth.signIn(email, password);
console.info("login success", user);
return {
success: true,
redirectTo: "/",
};
} catch (error) {
return getErrorResponse(error);
}
},
logout: async () => {
try {
await Auth.signOut();
return {
success: true,
redirectTo: "/login",
};
} catch (error) {
return getErrorResponse(error);
}
},
onError: async (error) => {
if (error.status === 401 || error.status === 403) {
return {
logout: true,
redirectTo: "/login",
error: new Error("Unauthorized"),
};
}
return {};
},
check: async () => {
try {
await Auth.currentSession();
return {
authenticated: true,
};
} catch (error) {
return {
authenticated: false,
logout: true,
redirectTo: "/login",
};
}
},
getPermissions: async () => {
try {
const user = (await Auth.currentAuthenticatedUser()) as unknown as CognitoUser;
// fixme: implement your own logic
// const permissions = (user.attributes["custom:permissions"] ?? "").split(",");
// return permissions;
return [];
} catch (error) {
return [];
}
},
getIdentity: async () => {
try {
const user = (await Auth.currentAuthenticatedUser()) as unknown as CognitoUser;
return {
id: user.username,
fullName: user.attributes.name,
avatar: user.attributes.picture,
};
} catch (error) {
return null;
}
},
forgotPassword: async (input) => {
try {
await Auth.forgotPassword(input.email);
return {
success: true,
redirectTo: "/update-password",
};
} catch (error) {
return getErrorResponse(error);
}
},
/**
* You will need to swizzle AuthPage, and edit inputs of UpdatePasswordPage with:
* type UpdatePasswordFormTypes = {
* email?: string;
* code?: string;
* password?: string;
* };
*/
updatePassword: async (input) => {
try {
await Auth.forgotPasswordSubmit(input.email, input.code, input.password);
return {
success: true,
redirectTo: "/login",
};
} catch (error) {
return getErrorResponse(error);
}
},
};
export { authProvider }; |
Beta Was this translation helpful? Give feedback.
For anyone looking for drop-in authProvider with amplify-js cognito integration: