Skip to content

Commit d6dbdc6

Browse files
committed
Restrict the sign-up page
1 parent 2a15637 commit d6dbdc6

File tree

16 files changed

+105
-33
lines changed

16 files changed

+105
-33
lines changed

proxy.conf.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { config } from '@dotenvx/dotenvx'
33
config({ quiet: true })
44

55
export default [{
6-
context: ['/api/v3/', '/api/token/', '/api/version/'],
6+
context: [
7+
'/api/config/',
8+
'/api/token/',
9+
'/api/v3/',
10+
'/api/version/'
11+
],
712
target: process.env.SEED_HOST ?? 'http://127.0.0.1:8000',
813
changeOrigin: true,
914
logLevel: 'debug',
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { HttpClient } from '@angular/common/http'
2+
import { inject, Injectable } from '@angular/core'
3+
import type { Observable } from 'rxjs'
4+
import { shareReplay } from 'rxjs'
5+
import type { ConfigResponse } from './config.types'
6+
7+
@Injectable({ providedIn: 'root' })
8+
export class ConfigService {
9+
private _httpClient = inject(HttpClient)
10+
11+
private _config$: Observable<ConfigResponse>
12+
13+
// Singleton request
14+
get config$(): Observable<ConfigResponse> {
15+
if (!this._config$) {
16+
this._config$ = this._httpClient.get<ConfigResponse>('/api/config/').pipe(shareReplay(1))
17+
}
18+
return this._config$
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type ConfigResponse = {
2+
allow_signup: boolean;
3+
}

src/@seed/api/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './config.service'
2+
export * from './config.types'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export type AlertAppearance = 'border' | 'fill' | 'outline' | 'soft'
22

33
export type AlertType = 'primary' | 'accent' | 'warn' | 'basic' | 'info' | 'success' | 'warning' | 'error'
4+
5+
export type Alert = { type: AlertType; message: string }

src/app/app.resolvers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { inject } from '@angular/core'
22
import { forkJoin } from 'rxjs'
3-
import { VersionService } from '@seed/api/version/version.service'
3+
import { ConfigService } from '@seed/api/config'
4+
import { VersionService } from '@seed/api/version'
5+
6+
export const configResolver = () => {
7+
const configService = inject(ConfigService)
8+
return configService.config$
9+
}
410

511
export const initialDataResolver = () => {
612
const versionService = inject(VersionService)

src/app/app.routes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Route, UrlSegment } from '@angular/router'
2-
import { initialDataResolver } from './app.resolvers'
2+
import { configResolver, initialDataResolver } from './app.resolvers'
33
import { AuthGuard, NoAuthGuard } from './core/auth'
44
import { LayoutComponent } from './layout/layout.component'
55
import { AboutComponent } from './modules/main/about/about.component'
@@ -31,6 +31,9 @@ export const appRoutes: Route[] = [
3131
canActivate: [NoAuthGuard],
3232
canActivateChild: [NoAuthGuard],
3333
component: LayoutComponent,
34+
resolve: {
35+
config: configResolver,
36+
},
3437
data: {
3538
layout: 'landing',
3639
},

src/app/core/auth/auth.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ export class AuthService {
3232
}
3333

3434
// TODO
35-
forgotPassword(email: string): Observable<any> {
35+
forgotPassword(email: string): Observable<unknown> {
3636
return this._httpClient.post('api/auth/forgot-password', email)
3737
}
3838

3939
// TODO
40-
resetPassword(password: string): Observable<any> {
40+
resetPassword(password: string): Observable<unknown> {
4141
return this._httpClient.post('api/auth/reset-password', password)
4242
}
4343

@@ -88,7 +88,7 @@ export class AuthService {
8888
}
8989

9090
// TODO
91-
signUp(user: { name: string; email: string; password: string; company: string }): Observable<any> {
91+
signUp(user: { name: string; email: string; password: string; company: string }): Observable<unknown> {
9292
return this._httpClient.post('api/auth/sign-up', user)
9393
}
9494

src/app/core/auth/guards/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './auth.guard'
22
export * from './noAuth.guard'
3+
export * from './signUp.guard'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { inject } from '@angular/core'
2+
import type { CanActivateFn } from '@angular/router'
3+
import { Router } from '@angular/router'
4+
import { map } from 'rxjs'
5+
import { ConfigService } from '@seed/api/config'
6+
7+
export const SignUpGuard: CanActivateFn = () => {
8+
const configService = inject(ConfigService)
9+
const router = inject(Router)
10+
11+
// Check the allowSignUp status
12+
return configService.config$.pipe(
13+
map(({ allow_signup: allowSignUp }) => {
14+
if (!allowSignUp) {
15+
// Redirect to the sign-in page
16+
return router.parseUrl('sign-in')
17+
}
18+
19+
// Allow the access
20+
return true
21+
}),
22+
)
23+
}

0 commit comments

Comments
 (0)