-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-test-user.sh
More file actions
executable file
·64 lines (56 loc) · 2.52 KB
/
Copy pathcreate-test-user.sh
File metadata and controls
executable file
·64 lines (56 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
# Creates (or resets the password of) the app login user, so you have
# credentials to sign in with at /login on a fresh (or already-tinkered-with)
# local Docker deployment.
#
# Works in two steps:
# 1. Upserts a PocketBase superuser via the `pocketbase superuser upsert`
# CLI (run inside the container), bypassing HTTP auth entirely.
# 2. Uses that superuser session to create or update the regular `users`
# collection record the app's /login page actually authenticates
# against (pb.collection('users').authWithPassword).
#
# Safe to re-run any time you want to reset the password back to the default.
set -euo pipefail
CONTAINER="${CONTAINER:-offcourselearn-app}"
PB_URL="${PB_URL:-http://localhost:5567}"
EMAIL="${TEST_USER_EMAIL:-username@example.com}"
PASSWORD="${TEST_USER_PASSWORD:-HXKh2x7z%genX}"
echo "Upserting PocketBase superuser inside ${CONTAINER}..."
docker exec "${CONTAINER}" /pb/pocketbase superuser upsert "${EMAIL}" "${PASSWORD}" --dir /pb_data
echo "Authenticating as superuser against ${PB_URL}..."
AUTH_RESPONSE="$(curl -sS -X POST "${PB_URL}/api/collections/_superusers/auth-with-password" \
-H 'Content-Type: application/json' \
-d "{\"identity\":\"${EMAIL}\",\"password\":\"${PASSWORD}\"}")"
TOKEN="$(echo "$AUTH_RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4)"
if [ -z "$TOKEN" ]; then
echo "Failed to authenticate as superuser:"
echo " ${AUTH_RESPONSE}"
exit 1
fi
echo "Looking up existing app user with email ${EMAIL}..."
EXISTING="$(curl -sS "${PB_URL}/api/collections/users/records?filter=email='${EMAIL}'" \
-H "Authorization: ${TOKEN}")"
RECORD_ID="$(echo "$EXISTING" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)"
if [ -n "$RECORD_ID" ]; then
echo "User exists (${RECORD_ID}) — resetting password..."
RESULT="$(curl -sS -X PATCH "${PB_URL}/api/collections/users/records/${RECORD_ID}" \
-H "Authorization: ${TOKEN}" \
-H 'Content-Type: application/json' \
-d "{\"password\":\"${PASSWORD}\",\"passwordConfirm\":\"${PASSWORD}\"}")"
else
echo "No existing user — creating one..."
RESULT="$(curl -sS -X POST "${PB_URL}/api/collections/users/records" \
-H "Authorization: ${TOKEN}" \
-H 'Content-Type: application/json' \
-d "{\"email\":\"${EMAIL}\",\"password\":\"${PASSWORD}\",\"passwordConfirm\":\"${PASSWORD}\"}")"
fi
if echo "$RESULT" | grep -q '"email"'; then
echo "Done. Log in at http://localhost:5566/login with:"
echo " Email: ${EMAIL}"
echo " Password: ${PASSWORD}"
else
echo "Failed:"
echo " ${RESULT}"
exit 1
fi