feat: add company selection to user dialog, require email password link on user creation, password validation with confirmation, and company grouping with search
Some checks failed
Staging Build / build (push) Failing after 25s
Some checks failed
Staging Build / build (push) Failing after 25s
This commit is contained in:
@@ -32,11 +32,11 @@ export async function getUsers() {
|
||||
}));
|
||||
}
|
||||
|
||||
export async function createUser(data: { email: string; password?: string; role?: 'admin' | 'partner'; email_confirm?: boolean; company_id?: string }) {
|
||||
export async function createUser(data: { email: string; role?: 'admin' | 'partner'; email_confirm?: boolean; company_id?: string }) {
|
||||
const admin = createAdminClient();
|
||||
const { data: { user }, error } = await admin.auth.admin.createUser({
|
||||
email: data.email,
|
||||
password: data.password || Math.random().toString(36).slice(-10),
|
||||
password: Math.random().toString(36).slice(-10) + Math.random().toString(36).slice(-10),
|
||||
email_confirm: data.email_confirm ?? true,
|
||||
});
|
||||
if (error) throw error;
|
||||
@@ -48,6 +48,29 @@ export async function createUser(data: { email: string; password?: string; role?
|
||||
.upsert({ id: user.id, role, company_id: data.company_id || null }, { onConflict: 'id' });
|
||||
if (dbError) throw dbError;
|
||||
|
||||
// Generate recovery link to let user set password
|
||||
try {
|
||||
const { data: linkData, error: linkError } = await admin.auth.admin.generateLink({
|
||||
type: 'recovery',
|
||||
email: data.email,
|
||||
});
|
||||
if (!linkError && linkData) {
|
||||
const tokenHash = (linkData as any)?.properties?.hashed_token;
|
||||
if (tokenHash) {
|
||||
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://staging.hephex.de';
|
||||
const setupLink = `${siteUrl}/auth/confirm?token_hash=${tokenHash}&type=recovery&next=/auth/update-password`;
|
||||
await sendMail({
|
||||
to: data.email,
|
||||
subject: 'Willkommen bei Hephex - Passwort erstellen',
|
||||
text: `Ihr Account wurde erstellt. Klicken Sie auf den folgenden Link, um Ihr Passwort festzulegen und Ihr Konto zu aktivieren: ${setupLink}`,
|
||||
html: `<p>Ihr Account wurde erstellt.</p><p>Klicken Sie <a href="${setupLink}">hier</a>, um Ihr Passwort festzulegen und Ihr Konto zu aktivieren.</p>`,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (mailError) {
|
||||
console.error('Failed to send welcome setup mail:', mailError);
|
||||
}
|
||||
|
||||
revalidatePath('/admin/users');
|
||||
return user;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user