Skip to content

Commit

Permalink
move autologin user to .env
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlstrm committed Jan 13, 2024
1 parent a28732e commit e2a553f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
9 changes: 5 additions & 4 deletions packages/cms/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ PAYLOAD_PUBLIC_FRONTEND_URL=http://localhost:3000
PAYLOAD_PRIVATE_REVALIDATION_KEY=EXAMPLE_REVALIDATION_KEY

# seed database when starting up for the first time.
# ONLY FOR DEV, do NOT do this for prod
# also sets up [email protected] account with autologin enabled
#PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_AND_SEEDING=true

# ONLY FOR DEV, do NOT do this for staging/prod
# also sets up account with autologin enabled, if not already set up
#PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT=true
#[email protected]
#PAYLOAD_PUBLIC_DEVELOPMENT_AUTOLOGIN_PASSWORD=root
8 changes: 5 additions & 3 deletions packages/cms/src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ export default buildConfig({
bundler: viteBundler(),
user: Users.slug,
autoLogin:
process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_AND_SEEDING === "true"
process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT === "true" &&
process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_EMAIL &&
process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_PASSWORD
? {
email: "[email protected]",
password: "root",
email: process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_EMAIL,
password: process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_PASSWORD,
}
: false,
},
Expand Down
30 changes: 29 additions & 1 deletion packages/cms/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,36 @@ const start = async () => {
await payload.init({
secret: process.env.PAYLOAD_SECRET!,
express: app,
onInit: (payload) => {
onInit: async (payload) => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
if (process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT === "true") {
const email = process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_EMAIL;
const password = process.env.PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_PASSWORD;
if (!email || !password) {
throw new Error(
"PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_EMAIL and PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT_PASSWORD must be set when PAYLOAD_PUBLIC_LOCAL_DEVELOPMENT is true",
);
}
// check if the user exists, if not, create it
const user = await payload.find({
collection: "users",
where: { email: { equals: email } },
});
if (user.totalDocs === 0) {
payload.logger.warn(`user ${email} not found, creating...`);
payload.logger.warn(
"NOTE that it is recommended to use the seeding scripts to a get filled database for local development",
);
await payload.create({
collection: "users",
data: {
email,
password,
},
});
payload.logger.warn("Payload autologin enabled!");
}
}
},
});

Expand Down

0 comments on commit e2a553f

Please sign in to comment.