Skip to content
Open

init #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,27 @@ export function createAuthModule(
* Login via username and password
* @param email - User email
* @param password - User password
* @param turnstileToken - Optional Turnstile captcha token
* @param options - Optional login options
* @param options.turnstileToken - Optional Turnstile captcha token
* @param options.fromUrl - Optional URL to redirect to after login
* @returns Login response with access_token and user
*/
async loginViaEmailPassword(
email: string,
password: string,
turnstileToken?: string
options?: {
turnstileToken?: string;
fromUrl?: string;
}
) {
try {
const response: { access_token: string; user: any } = await axios.post(
`/apps/${appId}/auth/login`,
{
email,
password,
...(turnstileToken && { turnstile_token: turnstileToken }),
...(options?.turnstileToken && { turnstile_token: options.turnstileToken }),
...(options?.fromUrl && { from_url: options.fromUrl }),
}
);

Expand Down
82 changes: 81 additions & 1 deletion tests/unit/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ describe('Auth Module', () => {
const result = await base44.auth.loginViaEmailPassword(
loginData.email,
loginData.password,
loginData.turnstile_token
{ turnstileToken: loginData.turnstile_token }
);

// Verify the response
Expand All @@ -481,6 +481,86 @@ describe('Auth Module', () => {
expect(scope.isDone()).toBe(true);
});

test('should login with fromUrl option when provided', async () => {
const loginData = {
email: '[email protected]',
password: 'password123'
};

const expectedPayload = {
email: '[email protected]',
password: 'password123',
from_url: 'https://example.com/dashboard'
};

const mockResponse = {
access_token: 'test-access-token',
user: {
id: 'user-123',
email: '[email protected]'
}
};

// Mock the API response
scope.post(`/api/apps/${appId}/auth/login`, expectedPayload)
.reply(200, mockResponse);

// Call the API with fromUrl option
const result = await base44.auth.loginViaEmailPassword(
loginData.email,
loginData.password,
{ fromUrl: 'https://example.com/dashboard' }
);

// Verify the response
expect(result.access_token).toBe('test-access-token');

// Verify all mocks were called
expect(scope.isDone()).toBe(true);
});

test('should login with multiple options', async () => {
const loginData = {
email: '[email protected]',
password: 'password123'
};

const expectedPayload = {
email: '[email protected]',
password: 'password123',
turnstile_token: 'turnstile-token-123',
from_url: 'https://example.com/dashboard'
};

const mockResponse = {
access_token: 'test-access-token',
user: {
id: 'user-123',
email: '[email protected]'
}
};

// Mock the API response
scope.post(`/api/apps/${appId}/auth/login`, expectedPayload)
.reply(200, mockResponse);

// Call the API with multiple options
const result = await base44.auth.loginViaEmailPassword(
loginData.email,
loginData.password,
{
turnstileToken: 'turnstile-token-123',
fromUrl: 'https://example.com/dashboard'
}
);

// Verify the response
expect(result.access_token).toBe('test-access-token');

// Verify all mocks were called
expect(scope.isDone()).toBe(true);
});

test('should handle authentication errors and logout', async () => {
const loginData = {
email: '[email protected]',
Expand Down