feat: implement login and signup functionality with auto-create option in stackAuthApi

This commit is contained in:
Zahid Ullah Khan 2026-01-21 16:41:02 +05:00
parent 21299788c3
commit ca26a7da87
6 changed files with 202 additions and 48 deletions

View file

@ -0,0 +1,44 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { loginByStackWithAutoCreate, loginByStackToken } from '../../../src/service/stackAuthApi'
vi.mock('@/api/http', () => ({
proxyFetchPost: vi.fn(),
}))
describe('stackAuthApi', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('falls back to signup when login returns user not found', async () => {
const { proxyFetchPost } = await import('@/api/http')
vi.mocked(proxyFetchPost)
.mockResolvedValueOnce({ code: 1, text: 'User not found' })
.mockResolvedValueOnce({ code: 0, token: 't', email: 'e@example.com' })
const res = await loginByStackWithAutoCreate('stack-token')
expect(res.code).toBe(0)
expect(vi.mocked(proxyFetchPost)).toHaveBeenCalledTimes(2)
const firstUrl = vi.mocked(proxyFetchPost).mock.calls[0][0] as string
const secondUrl = vi.mocked(proxyFetchPost).mock.calls[1][0] as string
expect(firstUrl).toContain('/api/login-by_stack?')
expect(firstUrl).toContain('type=login')
expect(secondUrl).toContain('type=signup')
})
it('includes invite_code in query when provided', async () => {
const { proxyFetchPost } = await import('@/api/http')
vi.mocked(proxyFetchPost).mockResolvedValueOnce({ code: 0 })
await loginByStackToken({ token: 'stack-token', type: 'signup', inviteCode: 'INV123' })
const url = vi.mocked(proxyFetchPost).mock.calls[0][0] as string
expect(url).toContain('invite_code=INV123')
})
})