Skip to content

Commit

Permalink
feat: add accounts js configuration as env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
tedraykov committed May 19, 2023
1 parent cd2f570 commit 13de0cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/api-plugin-authentication/src/config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import envalid from "envalid";

const { str } = envalid;
const { str, bool } = envalid;

export default envalid.cleanEnv(
process.env,
{
ACCOUNTS_JS_RETURN_TOKENS_AFTER_RESET_PASSWORD: bool({ default: false }),
ACCOUNTS_JS_ACCESS_TOKEN_EXPIRES_IN: str({ default: "90m" }),
ACCOUNTS_JS_REFRESH_TOKEN_EXPIRES_IN: str({ default: "30d" }),
PASSWORD_RESET_PATH_FRAGMENT: str({ default: "?resetToken=" }),
STORE_URL: str({ devDefault: "http://localhost:4000" }),
TOKEN_SECRET: str({ default: "UPDATE_THIS_SECRET" })
Expand Down
22 changes: 20 additions & 2 deletions packages/api-plugin-authentication/src/util/accountServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ export default async (app) => {
if (accountsServer && accountsGraphQL) {
return { accountsServer, accountsGraphQL };
}
const { MONGO_URL, PASSWORD_RESET_PATH_FRAGMENT, STORE_URL, TOKEN_SECRET } = config;
const {
ACCOUNTS_JS_RETURN_TOKENS_AFTER_RESET_PASSWORD,
ACCOUNTS_JS_ACCESS_TOKEN_EXPIRES_IN,
ACCOUNTS_JS_REFRESH_TOKEN_EXPIRES_IN,
MONGO_URL,
PASSWORD_RESET_PATH_FRAGMENT,
STORE_URL,
TOKEN_SECRET
} = config;
const { context } = app;

const client = await mongoConnectWithRetry(MONGO_URL);
Expand All @@ -26,12 +34,22 @@ export default async (app) => {
idProvider: () => mongoose.Types.ObjectId().toString()
});

const password = new AccountsPassword();
const password = new AccountsPassword({
returnTokensAfterResetPassword: ACCOUNTS_JS_RETURN_TOKENS_AFTER_RESET_PASSWORD
});

accountsServer = new AccountsServer(
{
siteUrl: STORE_URL,
tokenSecret: TOKEN_SECRET,
tokenConfigs: {
accessToken: {
expiresIn: ACCOUNTS_JS_ACCESS_TOKEN_EXPIRES_IN
},
refreshToken: {
expiresIn: ACCOUNTS_JS_REFRESH_TOKEN_EXPIRES_IN
}
},
db: accountsMongo,
enableAutologin: true,
ambiguousErrorMessages: false,
Expand Down

0 comments on commit 13de0cf

Please sign in to comment.