mirror of
https://github.com/kyantech/Palmr.git
synced 2025-11-03 05:23:19 +00:00
feat: enhance authentication flow and improve database setup script
- Added a check for first user access in the authentication context to handle initial user setup. - Updated the server start script to ensure proper ownership and permissions for database operations, enhancing compatibility with Docker environments. - Refactored database seeding and configuration checks to run as the target user, preventing permission issues during setup.
This commit is contained in:
@@ -41,6 +41,16 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const checkAuth = async () => {
|
const checkAuth = async () => {
|
||||||
try {
|
try {
|
||||||
|
const appInfoResponse = await fetch("/api/app/info");
|
||||||
|
const appInfo = await appInfoResponse.json();
|
||||||
|
|
||||||
|
if (appInfo.firstUserAccess) {
|
||||||
|
setUser(null);
|
||||||
|
setIsAdmin(false);
|
||||||
|
setIsAuthenticated(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const response = await getCurrentUser();
|
const response = await getCurrentUser();
|
||||||
if (!response?.data?.user) {
|
if (!response?.data?.user) {
|
||||||
throw new Error("No user data");
|
throw new Error("No user data");
|
||||||
|
|||||||
@@ -36,29 +36,49 @@ echo "💾 Database: $DATABASE_URL"
|
|||||||
echo "📁 Creating data directories..."
|
echo "📁 Creating data directories..."
|
||||||
mkdir -p /app/server/prisma /app/server/uploads /app/server/temp-chunks /app/server/uploads/logo
|
mkdir -p /app/server/prisma /app/server/uploads /app/server/temp-chunks /app/server/uploads/logo
|
||||||
|
|
||||||
|
# Fix ownership of database directory BEFORE database operations
|
||||||
|
if [ "$(id -u)" = "0" ]; then
|
||||||
|
echo "🔐 Ensuring proper ownership before database operations..."
|
||||||
|
chown -R $TARGET_UID:$TARGET_GID /app/server/prisma 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
|
||||||
# Check if it's a first run (no database file exists)
|
# Check if it's a first run (no database file exists)
|
||||||
if [ ! -f "/app/server/prisma/palmr.db" ]; then
|
if [ ! -f "/app/server/prisma/palmr.db" ]; then
|
||||||
echo "🚀 First run detected - setting up database..."
|
echo "🚀 First run detected - setting up database..."
|
||||||
|
|
||||||
# Create database with proper schema path
|
# Create database with proper schema path - run as target user to avoid permission issues
|
||||||
echo "🗄️ Creating database schema..."
|
echo "🗄️ Creating database schema..."
|
||||||
|
if [ "$(id -u)" = "0" ]; then
|
||||||
|
su-exec $TARGET_UID:$TARGET_GID npx prisma db push --schema=./prisma/schema.prisma --skip-generate
|
||||||
|
else
|
||||||
npx prisma db push --schema=./prisma/schema.prisma --skip-generate
|
npx prisma db push --schema=./prisma/schema.prisma --skip-generate
|
||||||
|
fi
|
||||||
|
|
||||||
# Run seed script from application directory (where node_modules is)
|
# Run seed script from application directory (where node_modules is) - as target user
|
||||||
echo "🌱 Seeding database..."
|
echo "🌱 Seeding database..."
|
||||||
|
if [ "$(id -u)" = "0" ]; then
|
||||||
|
su-exec $TARGET_UID:$TARGET_GID node ./prisma/seed.js
|
||||||
|
else
|
||||||
node ./prisma/seed.js
|
node ./prisma/seed.js
|
||||||
|
fi
|
||||||
|
|
||||||
echo "✅ Database setup completed!"
|
echo "✅ Database setup completed!"
|
||||||
else
|
else
|
||||||
echo "♻️ Existing database found"
|
echo "♻️ Existing database found"
|
||||||
|
|
||||||
# Always run migrations to ensure schema is up to date
|
# Always run migrations to ensure schema is up to date - as target user
|
||||||
echo "🔧 Checking for schema updates..."
|
echo "🔧 Checking for schema updates..."
|
||||||
|
if [ "$(id -u)" = "0" ]; then
|
||||||
|
su-exec $TARGET_UID:$TARGET_GID npx prisma db push --schema=./prisma/schema.prisma --skip-generate
|
||||||
|
else
|
||||||
npx prisma db push --schema=./prisma/schema.prisma --skip-generate
|
npx prisma db push --schema=./prisma/schema.prisma --skip-generate
|
||||||
|
fi
|
||||||
|
|
||||||
# Check if configurations exist
|
# Check if configurations exist - as target user
|
||||||
echo "🔍 Verifying database configurations..."
|
echo "🔍 Verifying database configurations..."
|
||||||
CONFIG_COUNT=$(node -e "
|
CONFIG_COUNT=$(
|
||||||
|
if [ "$(id -u)" = "0" ]; then
|
||||||
|
su-exec $TARGET_UID:$TARGET_GID node -e "
|
||||||
const { PrismaClient } = require('@prisma/client');
|
const { PrismaClient } = require('@prisma/client');
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
prisma.appConfig.count()
|
prisma.appConfig.count()
|
||||||
@@ -70,12 +90,32 @@ else
|
|||||||
console.log(0);
|
console.log(0);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
" 2>/dev/null || echo "0")
|
" 2>/dev/null || echo "0"
|
||||||
|
else
|
||||||
|
node -e "
|
||||||
|
const { PrismaClient } = require('@prisma/client');
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
prisma.appConfig.count()
|
||||||
|
.then(count => {
|
||||||
|
console.log(count);
|
||||||
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
console.log(0);
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
" 2>/dev/null || echo "0"
|
||||||
|
fi
|
||||||
|
)
|
||||||
|
|
||||||
if [ "$CONFIG_COUNT" -eq "0" ]; then
|
if [ "$CONFIG_COUNT" -eq "0" ]; then
|
||||||
echo "🌱 No configurations found, running seed..."
|
echo "🌱 No configurations found, running seed..."
|
||||||
# Always run seed from application directory where node_modules is available
|
# Always run seed from application directory where node_modules is available - as target user
|
||||||
|
if [ "$(id -u)" = "0" ]; then
|
||||||
|
su-exec $TARGET_UID:$TARGET_GID node ./prisma/seed.js
|
||||||
|
else
|
||||||
node ./prisma/seed.js
|
node ./prisma/seed.js
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
echo "✅ Found $CONFIG_COUNT configurations"
|
echo "✅ Found $CONFIG_COUNT configurations"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user