Skip to content

Commit

Permalink
feat(web): implement user authentication and add on the fly migration…
Browse files Browse the repository at this point in the history
… to Strapi's authentication
  • Loading branch information
izzyyhh committed May 7, 2024
1 parent 7ce4efa commit cca44f7
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DATABASE_PASSWORD=super-secret
DATABASE_PORT=3306
DATABASE_SSL=false

APP_DATABASE_NAME=vim
WEB_DATABASE_NAME=vim
CMS_DATABASE_NAME=vim_cms

CMS_PORT=1337
Expand Down
3 changes: 0 additions & 3 deletions cms/src/extensions/users-permissions/strapi-server.ts

This file was deleted.

140 changes: 140 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"@tabler/icons-react": "^2.45.0",
"@tanstack/react-table": "^8.11.7",
"axios": "^1.6.8",
"md5": "^2.3.0",
"mysql2": "^3.9.7",
"next": "13.5.6",
"next-auth": "^4.24.7",
"qs": "^6.11.2",
Expand All @@ -23,6 +25,7 @@
"swiper": "^11.0.5"
},
"devDependencies": {
"@types/md5": "^2.3.5",
"@types/node": "^20",
"@types/qs": "^6.9.11",
"@types/react": "^18",
Expand Down
35 changes: 26 additions & 9 deletions web/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import CredentialsProvider from "next-auth/providers/credentials";
import NextAuth from "next-auth/next";
import { signIn } from "@/lib/strapi/auth";
import { JWT } from "next-auth/jwt";
import { Session, User } from "next-auth";
import { signIn, signUp } from "@/lib/auth/strapi";
import { findUserByEmail, verifyPassword } from "@/lib/auth/legacy_web";
import { StrapiSignInResponse } from "@/lib/types";

const authOptions = {
Expand All @@ -13,13 +16,27 @@ const authOptions = {
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
console.log("heee");
try {
if (credentials?.email == null || credentials.password == null) return null;

const strapiResponse: StrapiSignInResponse = await signIn(credentials.email, credentials.password);

if (strapiResponse.error) {
if (strapiResponse === null || strapiResponse.error) {
// if strapi cannot authenticate
// look if user is to migrate
const potentialUserForMigration = await findUserByEmail(credentials.email);
const isToMigrate = potentialUserForMigration && verifyPassword(credentials.password, potentialUserForMigration.password);

if (isToMigrate) {
const migrationResponse = await signUp(potentialUserForMigration.user_name, potentialUserForMigration.email, credentials.password, "internal");

return {
jwt: migrationResponse.jwt,
id: String(migrationResponse.user.id),
email: migrationResponse.user.email,
name: migrationResponse.user.username,
};
}
return null;
}

Expand All @@ -29,20 +46,20 @@ const authOptions = {
email: strapiResponse.user.email,
name: strapiResponse.user.username,
};
} catch (e) {
console.log(e);
} catch {
return null;
}
},
}),
],
callbacks: {
session: async ({ session, token }: { session: any; token: any }) => {
session.id = token.id;
session.jwt = token.jwt;
session: async ({ session, token }: { session: Session; token: JWT }) => {
session.id = token.id as string;
session.jwt = token.jwt as string;

return session;
},
jwt: async ({ token, user }: { token: any; user: any }) => {
jwt: async ({ token, user }: { token: JWT; user: User }) => {
if (user) {
token.id = user.id;
token.jwt = user.jwt;
Expand Down
Loading

0 comments on commit cca44f7

Please sign in to comment.