Register endpoint PoC working

This commit is contained in:
Fergal Moran
2023-08-11 22:49:29 +01:00
parent 25a747fcfa
commit e38ec92997
5 changed files with 192 additions and 4 deletions

8
jest.config.cjs Normal file
View File

@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
};

View File

@@ -19,13 +19,23 @@
}
},
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"Next Auth",
"ASP.Net Core",
".NET Core",
"Identity"
]
],
"scripts": {
"clean": "echo Cleaning....",
"test": "pnpm test:IdentityEndpointsSample",
"test:IdentityEndpointsSample": "pnpm clean && ./tests/IdentityEndpointsSample/test.sh"
},
"dependencies": {
"next-auth": "^4.23.0"
},
"devDependencies": {
"@types/jest": "^29.5.3",
"jest": "^29.6.2",
"ts-jest": "^29.1.1"
}
}

75
src/index.ts Normal file
View File

@@ -0,0 +1,75 @@
import {type AdapterUser, type Adapter} from "next-auth/adapters";
import {fixtures} from "../tests/fixtures";
export function AspNetIdentityAdapter(baseUrl: string): Adapter {
return {
async createUser(data: AdapterUser) {
const url = `${baseUrl}/register`;
const result = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: fixtures.user.username,
email: fixtures.user.email,
password: fixtures.user.password
}),
});
if (result.status === 200) {
//TODO: register endpoint should return more than a 200
return {
id: '1409b076-296d-481e-9f52-1996b8002d9c',
username: data.name,
email: data.email,
emailVerified: new Date()
} as AdapterUser;
} else {
const body = await result.json()
if (result.status === 400) {
console.log("authService", "registerUser", body);
}
throw Error(`Unable to create user: result ${body}`);
}
},
async getUser(id) {
throw Error(`Not implemented`);
},
async getUserByEmail(email) {
throw Error(`Not implemented`);
},
async getUserByAccount({providerAccountId, provider}) {
throw Error(`Not implemented`);
},
async updateUser(user) {
throw Error(`Not implemented`);
},
async deleteUser(userId) {
throw Error(`Not implemented`);
},
async linkAccount(account) {
throw Error(`Not implemented`);
},
async unlinkAccount({providerAccountId, provider}) {
throw Error(`Not implemented`);
},
async createSession({sessionToken, userId, expires}) {
throw Error(`Not implemented`);
},
async getSessionAndUser(sessionToken) {
throw Error(`Not implemented`);
},
async updateSession({sessionToken}) {
throw Error(`Not implemented`);
},
async deleteSession(sessionToken) {
throw Error(`Not implemented`);
},
async createVerificationToken({identifier, expires, token}) {
throw Error(`Not implemented`);
},
async useVerificationToken({identifier, token}) {
throw Error(`Not implemented`);
},
};
}

70
tests/index.ts Normal file
View File

@@ -0,0 +1,70 @@
import {Adapter} from "next-auth/adapters";
import {fixtures} from "./fixtures";
export interface TestOptions {
adapter: Adapter
fixtures?: {
user?: any
session?: any
account?: any
sessionUpdateExpires?: Date
verificationTokenExpires?: Date
},
skipTests?: string[]
}
/**
* A wrapper to run the most basic tests.
* Run this at the top of your test file.
* You can add additional tests below, if you wish.
*/
export async function runBasicTests(options: TestOptions) {
const {adapter: _adapter, skipTests} = options
const adapter = _adapter as Required<Adapter>
// Init
beforeAll(async () => {
console.log('Tests', 'beforeAll')
})
afterAll(async () => {
console.log('Tests', 'afterAll')
})
let user: any = options.fixtures?.user ?? {
email: "fill@murray.com",
image: "https://www.fillmurray.com/460/300",
name: "Fill Murray",
emailVerified: new Date()
}
// All adapters must define these methods
test("Required (User, Account, Session) methods exist", () => {
const requiredMethods = [
"createUser",
"getUser",
"getUserByEmail",
"getUserByAccount",
"updateUser",
"linkAccount",
"createSession",
"getSessionAndUser",
"updateSession",
"deleteSession",
]
requiredMethods.forEach((method) => {
expect(adapter).toHaveProperty(method)
})
})
test("createUser", async () => {
const result = await adapter.createUser(user)
console.log('tests', 'createUser', result)
expect(result.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
expect(result.email).toMatch(fixtures.user.email)
})
test("getUserByEmail", async () => {
const result = await adapter.getUserByEmail(fixtures.user.email);
})
}

25
tsconfig.json Normal file
View File

@@ -0,0 +1,25 @@
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"isolatedModules": true,
"esModuleInterop": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"outDir": ".",
"rootDir": "src",
"skipDefaultLibCheck": true,
"strictNullChecks": true,
"stripInternal": true,
"declarationMap": true,
"declaration": true
},
"include": [
"src/**/*"
],
"exclude": [
"*.js",
"*.d.ts"
]
}