Skip to content

Commit

Permalink
Merge pull request #122 from Mintplex-Labs/121-improved-onboarding
Browse files Browse the repository at this point in the history
improve onboarding to auto-onboard
  • Loading branch information
timothycarambat committed Jan 19, 2024
2 parents 2174e93 + bbda8cf commit e3c48b1
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ In separate terminal windows from project root:
- `yarn dev:workers`
- `cd document-processor && flask run --host '0.0.0.0' --port 8888`

On first boot of the system you will be prompted to login. Consult the `backend/.env.development` and set or use the `SYS_EMAIL` and `SYS_PASSWORD` values. Once your new account is setup the root credentials will no longer work and you can use your admin account.
On first boot and visiting of the homepage, you will be automatically redirected to create your primary admin account, organization, and database connection.

## Contributing
- create issue
Expand Down
5 changes: 0 additions & 5 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
SERVER_PORT=3001

# temp Root account login
SYS_EMAIL=[email protected]
SYS_PASSWORD=password

JWT_SECRET="YoUr_RaNd0M_sTr1nG"
# STORAGE_DIR=
DATABASE_CONNECTION_STRING="postgresql://user:password@host:port/database"
30 changes: 30 additions & 0 deletions backend/endpoints/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@ const bcrypt = require("bcrypt");
function authenticationEndpoints(app) {
if (!app) return;

app.get("/auth/auto-onboard", async (_, response) => {
try {
const completeSetup = (await User.count({ role: "admin" })) > 0;
if (completeSetup) {
response.status(200).json({ completed: true });
return;
}

const onboardingUser = await User.get({ role: "root" });
if (!onboardingUser) {
response.status(200).json({ completed: true });
return;
}

await Telemetry.sendTelemetry("onboarding_complete"); // Have to send here since we have no other hooks.
response.status(200).json({
valid: true,
user: onboardingUser,
token: makeJWT(
{ id: onboardingUser.id, email: onboardingUser.email },
"1hr"
),
message: null,
});
} catch (e) {
console.log(e.message, e);
response.sendStatus(500).end();
}
});

app.post("/auth/login", async (request, response) => {
try {
const { email, password } = reqBody(request);
Expand Down
4 changes: 2 additions & 2 deletions backend/utils/boot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ async function systemInit() {
const completeSetup = (await User.count({ role: "admin" })) > 0;
if (completeSetup) return;

process.env.SYS_EMAIL = process.env.SYS_EMAIL ?? "[email protected]";
process.env.SYS_PASSWORD = process.env.SYS_PASSWORD ?? "password";
process.env.SYS_EMAIL = "[email protected]";
process.env.SYS_PASSWORD = "password";

const existingRootUser = await User.get({
email: process.env.SYS_EMAIL,
Expand Down
3 changes: 0 additions & 3 deletions docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ SERVER_PORT=3001
# STORAGE_DIR="./backend/storage"

JWT_SECRET="your-random-string-here"
SYS_EMAIL="[email protected]"
SYS_PASSWORD="hunter2"

INNGEST_EVENT_KEY="background_workers"
INNGEST_SIGNING_KEY="random-string-goes-here"
INNGEST_LANDING_PAGE="true"
Expand Down
6 changes: 1 addition & 5 deletions docker/DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ Run this command first to get a dockerized Postgres container running:
`docker run -d -p 3001:3001 \
-e SERVER_PORT="3001" \
-e JWT_SECRET="your-random-string-here" \
-e SYS_EMAIL="[email protected]" \
-e SYS_PASSWORD="password" \
-e INNGEST_EVENT_KEY="background_workers" \
-e INNGEST_SIGNING_KEY="random-string-goes-here" \
-e INNGEST_LANDING_PAGE="true" \
Expand All @@ -36,8 +34,6 @@ mintplexlabs/vectoradmin`
- Edit `.env` file and update the variables. **please** update all of the following:
```shell
JWT_SECRET="some-random-string"
SYS_EMAIL="[email protected]"
SYS_PASSWORD="password"
DATABASE_CONNECTION_STRING="postgresql://vectoradmin:[email protected]:5433/vdbms" # Valid PG Connection string.
INNGEST_SIGNING_KEY="some-random-string"
```
Expand All @@ -46,7 +42,7 @@ INNGEST_SIGNING_KEY="some-random-string"

## How to use the user interface and login for the first time.
- To access the full application, visit `http://localhost:3001` in your browser.
- You first login will require you to use the `SYS_EMAIL` and `SYS_PASSWORD` set via ENV during build or run. After onboarding this login will be permanently disabled.
- You will automatically be redirected into onboarding to create your primary admin account, organization, and vector database connection.

# Connecting to a Vector Database

Expand Down
27 changes: 26 additions & 1 deletion frontend/src/models/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import { API_BASE } from '../utils/constants';
import { API_BASE, COMPLETE_ONBOARDING } from '../utils/constants';
import { baseHeaders } from '../utils/request';

const User = {
autoOnboard: async (): Promise<{
user: object | null;
token: string | null;
}> => {
if (!!window.localStorage.getItem(COMPLETE_ONBOARDING))
return { user: null, token: null };
return fetch(`${API_BASE}/auth/auto-onboard`, {
method: 'GET',
cache: 'no-cache',
})
.then((res) => res.json())
.then((res) => {
// If special key for `completed` is set, we will stop checking this for the client.
if (res.hasOwnProperty('completed')) {
window.localStorage.setItem(COMPLETE_ONBOARDING, 'true');
return { user: null, token: null };
}

return res;
})
.catch((e) => {
console.error(e);
return { user: null, token: null };
});
},
login: async (email: string, password: string) => {
let error;
const { user, valid, token } = await fetch(`${API_BASE}/auth/login`, {
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/pages/Authentication/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import User from '../../models/user';
import { APP_NAME, STORE_TOKEN, STORE_USER } from '../../utils/constants';
import paths from '../../utils/paths';
import validateSessionTokenForUser from '../../utils/session';
import System from '../../models/system';

type IStages = 'loading' | 'failed' | 'success' | 'ready';
type FormTypes = {
Expand Down Expand Up @@ -69,8 +70,23 @@ const SignIn = () => {
return false;
}
window.location.replace(paths.dashboard());
return true;
}
checkAuth();

async function autoOnboard() {
const { user, token } = await User.autoOnboard();
if (!!token && !!user) {
window.localStorage.setItem(STORE_USER, JSON.stringify(user));
window.localStorage.setItem(STORE_TOKEN, token);
window.location.replace(paths.onboardingSetup());
}
return;
}

checkAuth().then((res) => {
if (res) return;
autoOnboard();
});
}, []);

return (
Expand Down
1 change: 1 addition & 0 deletions frontend/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const APP_NAME = import.meta.env.VITE_APP_NAME || 'VDMS';
export const STORE_USER = 'vdms_user';
export const STORE_TOKEN = 'vdms_authToken';
export const COMPLETE_QUESTIONNAIRE = 'vectoradmin_completed_questionnaire';
export const COMPLETE_ONBOARDING = 'vectoradmin_completed_onboarding';
export const SUPPORTED_VECTOR_DBS = [
'pinecone',
'chroma',
Expand Down

0 comments on commit e3c48b1

Please sign in to comment.