├── src ├── assets │ └── .gitkeep ├── app │ ├── admin │ │ ├── index.ts │ │ ├── admin.component.html │ │ └── admin.component.ts │ ├── home │ │ ├── index.ts │ │ ├── home.component.html │ │ └── home.component.ts │ ├── login │ │ ├── index.ts │ │ ├── login.component.html │ │ └── login.component.ts │ ├── _models │ │ ├── index.ts │ │ ├── role.ts │ │ └── user.ts │ ├── _services │ │ ├── index.ts │ │ ├── user.service.ts │ │ └── authentication.service.ts │ ├── _helpers │ │ ├── index.ts │ │ ├── error.interceptor.ts │ │ ├── jwt.interceptor.ts │ │ ├── auth.guard.ts │ │ └── fake-backend.ts │ ├── app.component.ts │ ├── app-routing.module.ts │ ├── app.component.html │ └── app.module.ts ├── styles.less ├── favicon.ico ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── main.ts ├── index.html ├── test.ts └── polyfills.ts ├── .vscode ├── extensions.json ├── launch.json └── tasks.json ├── README.md ├── .editorconfig ├── tsconfig.app.json ├── tsconfig.spec.json ├── .browserslistrc ├── .gitignore ├── LICENSE ├── tsconfig.json ├── package.json ├── karma.conf.js └── angular.json /src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/admin/index.ts: -------------------------------------------------------------------------------- 1 | export * from './admin.component'; -------------------------------------------------------------------------------- /src/app/home/index.ts: -------------------------------------------------------------------------------- 1 | export * from './home.component'; -------------------------------------------------------------------------------- /src/app/login/index.ts: -------------------------------------------------------------------------------- 1 | export * from './login.component'; -------------------------------------------------------------------------------- /src/app/_models/index.ts: -------------------------------------------------------------------------------- 1 | export * from './role'; 2 | export * from './user'; -------------------------------------------------------------------------------- /src/app/_models/role.ts: -------------------------------------------------------------------------------- 1 | export enum Role { 2 | User = 'User', 3 | Admin = 'Admin' 4 | } -------------------------------------------------------------------------------- /src/styles.less: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ -------------------------------------------------------------------------------- /src/app/_services/index.ts: -------------------------------------------------------------------------------- 1 | export * from './authentication.service'; 2 | export * from './user.service'; -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cornflourblue/angular-14-role-based-authorization-example/HEAD/src/favicon.ico -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true, 3 | apiUrl: 'http://localhost:4000' 4 | }; 5 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846 3 | "recommendations": ["angular.ng-template"] 4 | } 5 | -------------------------------------------------------------------------------- /src/app/_helpers/index.ts: -------------------------------------------------------------------------------- 1 | export * from './auth.guard'; 2 | export * from './error.interceptor'; 3 | export * from './fake-backend'; 4 | export * from './jwt.interceptor'; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # angular-14-role-based-authorization-example 2 | 3 | Angular 14 - Role Based Authorization Tutorial with Example 4 | 5 | Documentation at https://jasonwatmore.com/post/2022/12/22/angular-14-role-based-authorization-tutorial-with-example -------------------------------------------------------------------------------- /src/app/_models/user.ts: -------------------------------------------------------------------------------- 1 | import { Role } from "./role"; 2 | 3 | export interface User { 4 | id: number; 5 | firstName: string; 6 | lastName: string; 7 | username: string; 8 | role: Role; 9 | token?: string; 10 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/app", 6 | "types": [] 7 | }, 8 | "files": [ 9 | "src/main.ts", 10 | "src/polyfills.ts" 11 | ], 12 | "include": [ 13 | "src/**/*.d.ts" 14 | ] 15 | } -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |This page can be accessed only by administrators.
5 |All users from secure (admin only) api end point:
6 | 7 |You're logged in with Angular 14 & JWT!!
5 |Your role is: {{user.role}}.
6 |This page can be accessed by all authenticated users.
7 |Current user from secure api end point:
8 | 9 |18 | Angular 14 - Role Based Authorization Tutorial with Example 19 |
20 |21 | JasonWatmore.com 22 |
23 |