Check the live demo: angular-login-demo.netlify.app
This repo is a minimal Angular app demonstrating login, signup, route protection, and HTTP interceptor usage against the public reqres.in API.
- Login & Signup using
reqres.infake API - Auth state tracking with
BehaviorSubject - Route guard to protect authenticated routes
- HTTP interceptor to attach headers and control caching
- Cookie storage demo for a simple session token
- Angular 18 (CLI 18)
- RxJS 7
- Angular Material/CDK
ngx-cookie-service
- Node.js 18+ recommended
- npm 9+
git clone https://github.com/akshay-js/login-in-angular.git
cd login-in-angular
npm install
npm startThen open http://localhost:4200/.
npm start: run dev server (ng serve)npm run build: production build todist/npm test: unit tests (Karma)npm run e2e: end-to-end (Protractor)
- The app uses
reqres.inendpoints:POST https://reqres.in/api/loginfor loginPOST https://reqres.in/api/registerfor signup
- On successful login/register,
reqres.inreturns a JSON{ token: string }. ApiServiceupdates an internalBehaviorSubject<boolean>to reflect login status and demonstrates setting a cookie namedcurrentUser.ApiInterceptorattaches headers to every request:x-authfrom thecurrentUsercookie (if present)x-api-key: reqres-free-v1forreqres.incalls- cache-control headers to avoid stale responses
AuthGuarduses the auth state to allow/deny navigation to protected routes (e.g., dashboard/profile).
Notes:
- This demo intentionally keeps auth simple and client-side only. Do not use as-is for production.
- The
currentUsercookie is a simple demo placeholder; in real apps, use secure, httpOnly cookies or other robust storage with a proper backend.
src/app/login: login form and flowsrc/app/signup: signup form and flowsrc/app/profile/dashboard: an example protected areasrc/app/auth/api.service.ts: API calls, auth state, logoutsrc/app/auth/api.interceptor.ts: request headers and cache controlsrc/app/auth/auth.guard.ts: route protection
- Start the dev server.
- Go to Login and use any valid
reqres.incredentials. Example (from their docs):- Login: email
eve.holt@reqres.in, passwordcityslicka - Signup: email
eve.holt@reqres.in, passwordpistol
- Login: email
- After login, navigate to the dashboard to see a protected view.
- Use the header to logout.
This repo includes a _redirects file for SPA routing (in src/_redirects, copied to dist/ on build).
Steps:
npm run build- Deploy the
dist/login-in-angularfolder to Netlify - Ensure the
_redirectsfile is present at the site root with this content:
/* /index.html 200
- If you see 404s on refresh in production hosting, confirm the
_redirectsfile is present. - If login seems not to persist after a full page reload, remember this is a minimal demo using a simple cookie; real apps require a proper session/JWT strategy with refresh/renewal.
- Make sure you are using supported Node and npm versions.
Issues and PRs are welcome! For major changes, please open an issue first to discuss what you would like to change.
MIT