better-auth-university 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Jaren Goldberg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Better Auth University Plugin
2
+
3
+ A university plugin for Better Auth that allows restriction of email domains.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install better-auth-university
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ To use the Better Auth University Plugin, you need to configure it with a list of universities and their associated email domains. You can also specify allowed email domains for user registration. If you would like a comprehensive list of universities, consider going to the [university-domains-list](https://github.com/Hipo/university-domains-list) for options of obtaining the data. There is a world_universities_and_domains.json which can be easily passed to the `university` parameter.
14
+
15
+ ```typescript
16
+ import { universityResolver } from "better-auth-university";
17
+
18
+ const plugin = universityResolver({
19
+ universities: [
20
+ {
21
+ name: "Massachusetts Institute of Technology",
22
+ domains: ["mit.edu"],
23
+ web_pages: ["http://web.mit.edu/"],
24
+ country: "United States",
25
+ alpha_two_code: "US",
26
+ "state-province": "Massachusetts",
27
+ },
28
+ ],
29
+ allowedEmailDomains: [".edu", ".ac.uk"],
30
+ });
31
+ ```
@@ -0,0 +1,27 @@
1
+ import { BetterAuthPlugin } from 'better-auth';
2
+ export interface University {
3
+ id: string;
4
+ name: string;
5
+ domain: string;
6
+ }
7
+ export interface UniversityData {
8
+ name: string;
9
+ domains: string[];
10
+ web_pages: string[];
11
+ country: string;
12
+ alpha_two_code: string;
13
+ "state-province": string | null;
14
+ }
15
+ export interface UniversityResolverOptions {
16
+ universities: UniversityData[];
17
+ /**
18
+ * List of allowed email domains or extensions.
19
+ *
20
+ * Examples: ['.edu', '.ac.uk', 'mit.edu']
21
+ * If not provided, defaults to ['.edu'].
22
+ *
23
+ * Pass `['*']` to allow all domains.
24
+ */
25
+ allowedEmailDomains?: string[];
26
+ }
27
+ export declare const universityResolver: (options: UniversityResolverOptions) => BetterAuthPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ import { createAuthMiddleware, APIError } from 'better-auth/api';
2
+ import { mergeSchema } from 'better-auth/db';
3
+ import { getSchema } from './schema.js';
4
+ export const universityResolver = (options) => {
5
+ const allowedDomains = options.allowedEmailDomains || ['.edu'];
6
+ const allowAll = allowedDomains.includes('*');
7
+ return {
8
+ id: 'university-resolver',
9
+ schema: mergeSchema(getSchema()),
10
+ hooks: {
11
+ before: [
12
+ {
13
+ matcher: (context) => {
14
+ return context.path === '/sign-up/email';
15
+ },
16
+ handler: createAuthMiddleware(async (context) => {
17
+ const { email } = context.body;
18
+ if (!email || typeof email !== 'string') {
19
+ return;
20
+ }
21
+ if (!allowAll) {
22
+ const isValid = allowedDomains.some(domain => email.endsWith(domain));
23
+ if (!isValid) {
24
+ throw new APIError("BAD_REQUEST", {
25
+ message: `Email must end with one of the following: ${allowedDomains.join(', ')}`
26
+ });
27
+ }
28
+ }
29
+ const emailDomain = email.split('@')[1];
30
+ const adapter = context.context.adapter;
31
+ let university = await adapter.findOne({
32
+ model: "university",
33
+ where: [
34
+ {
35
+ field: "domain",
36
+ value: emailDomain
37
+ }
38
+ ]
39
+ });
40
+ if (!university) {
41
+ const found = options.universities.find(university => university.domains.includes(emailDomain));
42
+ if (found) {
43
+ university = await adapter.create({
44
+ model: "university",
45
+ data: {
46
+ name: found.name,
47
+ domain: emailDomain,
48
+ }
49
+ });
50
+ }
51
+ else {
52
+ university = await adapter.create({
53
+ model: "university",
54
+ data: {
55
+ name: emailDomain,
56
+ domain: emailDomain,
57
+ }
58
+ });
59
+ }
60
+ }
61
+ if (university) {
62
+ context.body.universityId = university.id;
63
+ }
64
+ })
65
+ }
66
+ ]
67
+ },
68
+ };
69
+ };
@@ -0,0 +1,31 @@
1
+ export declare const getSchema: () => {
2
+ user: {
3
+ fields: {
4
+ universityId: {
5
+ type: "string";
6
+ required: false;
7
+ defaultValue: null;
8
+ returned: true;
9
+ references: {
10
+ model: string;
11
+ field: string;
12
+ };
13
+ };
14
+ };
15
+ };
16
+ university: {
17
+ fields: {
18
+ name: {
19
+ type: "string";
20
+ required: true;
21
+ returned: true;
22
+ };
23
+ domain: {
24
+ type: "string";
25
+ required: true;
26
+ unique: true;
27
+ returned: true;
28
+ };
29
+ };
30
+ };
31
+ };
package/dist/schema.js ADDED
@@ -0,0 +1,33 @@
1
+ export const getSchema = () => {
2
+ return {
3
+ user: {
4
+ fields: {
5
+ universityId: {
6
+ type: "string",
7
+ required: false,
8
+ defaultValue: null,
9
+ returned: true,
10
+ references: {
11
+ model: "university",
12
+ field: "id",
13
+ },
14
+ },
15
+ },
16
+ },
17
+ university: {
18
+ fields: {
19
+ name: {
20
+ type: "string",
21
+ required: true,
22
+ returned: true,
23
+ },
24
+ domain: {
25
+ type: "string",
26
+ required: true,
27
+ unique: true,
28
+ returned: true,
29
+ },
30
+ },
31
+ },
32
+ };
33
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "better-auth-university",
3
+ "version": "0.1.0",
4
+ "description": "A university plugin for Better Auth that allows restriction of email domains.",
5
+ "author": "Jaren Goldberg <fyrlex@gmail.com>",
6
+ "main": "dist/index.js",
7
+ "type": "module",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "require": "./dist/index.js"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/LuyxLLC/better-auth-university.git"
27
+ },
28
+ "keywords": [
29
+ "better-auth",
30
+ "auth",
31
+ "plugin",
32
+ "university"
33
+ ],
34
+ "license": "MIT",
35
+ "bugs": {
36
+ "url": "https://github.com/LuyxLLC/better-auth-university/issues"
37
+ },
38
+ "homepage": "https://github.com/LuyxLLC/better-auth-university#readme",
39
+ "peerDependencies": {
40
+ "better-auth": "^1.4.5"
41
+ },
42
+ "devDependencies": {
43
+ "typescript": "^5.9.3"
44
+ }
45
+ }