Auth MFE Blog #1 — Introduction, Functional Scope, Technical Scope & React Primer
Auth MFE Blog #1 — Introduction, Functional Scope, Technical Scope & React Primer
Supportive, step-by-step path to a tiny authentication micro-frontend using Vite, React, TypeScript, React Router, and MSW. No backend or tests required to begin.
Table of Contents
#intro — Introduction
What is an Auth MFE? An authentication micro-frontend is a small, independent web app that focuses on login, signup, and simple user flows. It can be built, tested, and deployed by a team on its own, then embedded into bigger portals.
Why micro-frontends? They reduce risk and speed up delivery by letting teams ship smaller pieces independently. Each MFE stays tidy and easier to reason about.
Series outcome: You’ll build a tiny Auth MFE with React + TypeScript + Router, powered by fake APIs via MSW (Mock Service Worker). We start small, learn safely, and grow features step by step.
- Auth MFE blog #1 (this post): scope, stack, mental model, quick setup.
- Auth MFE blog #2: we implement the scope in 10 incremental examples.
- Auth MFE blog #3: we add tests and a beginner testing mindset.
#functional-scope — Functional Scope
Features in scope
- Routes: /login, /signup, /users (list only)
- Basic navigation between routes
- Fake success / error states (no real auth)
- No backend — we simulate with MSW
Out of scope (for now)
- Security hardening, real tokens, persistence
- Design systems / advanced accessibility
- Complex state management or caching
Heads-up: Real systems must handle token storage, refresh, expiry, secure routes, and persistence. We’ll learn the UI flow first, safely.
#technical-scope — Technical Scope
Stack
- Vite — super-fast dev server & build tool.
- React — component-based UI library.
- TypeScript — types for safer code and better editor hints.
- React Router — routes like /login inside a single-page app.
- MSW — intercepts network calls and returns mock JSON.
Project anatomy (typical)
- index.html — page shell where Vite injects your app.
- src/main.tsx — React entry (bootstraps the app).
- src/App.tsx — top-level routes / layout.
- src/pages/* — /login, /signup, /users.
- src/mocks/* — MSW setup & handlers (fake APIs).
Dev server & environment notes
- Vite dev server runs on http://localhost:5173 by default.
- Environment vars live in .env and must start with VITE_ (e.g., VITE_API_BASE).
Why MSW? It fakes a backend in the browser so you can build UI today.
Why TypeScript? It catches mistakes early and explains APIs as you type—great for beginners.
Why Router? It teaches the SPA mental model: one page, many views controlled by the URL.
#primer — React Detailed Primer (10-minute)
How to read this: Each item is 1–2 sentences with a tiny “code-ish” line. Copy, run, then tweak one thing—small wins build confidence!
- Components: Reusable UI blocks (like Lego). Your app is a tree of components. function Button(){ return <button>Click</button> }
- JSX: HTML-like syntax inside JS/TS that compiles to React calls. const view = (<h1>Hello</h1>)
- State: Component memory for changing data. const [count,setCount]=useState(0)
- Props: Inputs to a component (like function args). <Avatar name="Ada" />
- Events: Wire browser events via props. <button onClick={()=>setOpen(true)}>Open</button>
- Effects: Run side-effects after render. useEffect(()=>{ /* fetch */ },[])
- Routing: Map URLs to components for SPA pages. <Route path="/login" element={<Login/>} />
- Declarative vs Imperative: Describe what UI should look like, not step-by-step DOM edits.
- Controlled forms: Input value lives in state. <input value={email} onChange={e=>setEmail(e.target.value)} />
- Mocking (MSW): Pretend server replies to unblock UI work. /api/login -> {"ok": true}
- Beginner testing mindset (short): Ask “what should the user see/do?” We verify in Auth MFE blog #3.
Micro-examples (copy/paste)
1) React “Hello, World!”
// src/HelloWorld.tsx
export default function HelloWorld(){
return <h1>Hello, World!</h1>;
}
// Usage (in App.tsx)
/*
import HelloWorld from "./HelloWorld";
export default function App(){
return (<main><HelloWorld /></main>);
}
*/
2) Tiny counter (state + event)
// src/Counter.tsx
import { useState } from "react";
export default function Counter(){
const [n,setN] = useState(0);
return (<div>
<p>Count: {n}</p>
<button onClick={()=>setN(n+1)}>+1</button>
</div>);
}
3) Controlled input (form sync)
// src/EmailInput.tsx
import { useState } from "react";
export default function EmailInput(){
const [email,setEmail] = useState("");
return (<label>Email:
<input value={email} onChange={e=>setEmail(e.target.value)} />
</label>);
}
4) Mini “Login” (fake submit)
// src/pages/Login.tsx
import { useState } from "react";
export default function Login(){
const [email,setEmail] = useState("");
const [pass,setPass] = useState("");
const [msg,setMsg] = useState("");
function onSubmit(e:any){
e.preventDefault();
// Pretend server call; MSW will handle in Auth MFE blog #2
setMsg(email ? "Welcome back!" : "Enter email");
}
return (<form onSubmit={onSubmit}>
<h2>Login</h2>
<input placeholder="Email" value={email} onChange={e=>setEmail(e.target.value)} />
<input placeholder="Password" type="password" value={pass} onChange={e=>setPass(e.target.value)} />
<button type="submit">Sign in</button>
{msg && <p>{msg}</p>}
</form>);
}
5) Routes sketch
// src/App.tsx (sketch)
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
import Login from "./pages/Login";
function Signup(){ return <h2>Signup</h2>; }
function Users(){ return <h2>Users (list)</h2>; }
export default function App(){
return (<BrowserRouter>
<nav style={{display:"flex",gap:"1rem"}}>
<Link to="/login">Login</Link>
<Link to="/signup">Signup</Link>
<Link to="/users">Users</Link>
</nav>
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/signup" element={<Signup />} />
<Route path="/users" element={<Users />} />
</Routes>
</BrowserRouter>);
}
Practice path: Paste one example, run it, then tweak labels or behavior. Immediate feedback cements understanding.
#diagram — Architecture Diagram
This inline SVG renders without any scripts (Blogger-safe). It mirrors the intended Mermaid diagram: React App → fetch → MSW handlers → JSON back, with visible routes.
If you prefer an image: export this diagram as PNG/SVG and insert via Blogger’s “Insert image”.
#prereqs — Prerequisites & Setup (PowerShell)
- Node.js 18+ (prefer 20)
- Git (optional but handy)
- Editor: VS Code or IntelliJ IDEA
Quick verify
# Check Node & npm
node -v
npm -v
# Optional: Git
git --version
Works anywhere: Windows (PowerShell), macOS, Linux. If commands fail, restart the terminal so PATH updates apply.
#workspace — Create Workspace & First Project
1) Scaffold Vite + React + TypeScript
# Creates a new project folder named auth-mfe
npm create vite@latest auth-mfe -- --template react-ts
# Move in & install
cd auth-mfe
npm install
During scaffolding: If prompted, choose React + TypeScript.
2) Add Router and MSW (wired in Auth MFE blog #2)
npm install react-router-dom
npm install --save-dev msw
3) Start the dev server
npm run dev
Open the printed URL (usually http://localhost:5173). If port is busy: npm run dev -- --port=5174.
Small win: Edit src/App.tsx text—see instant refresh. That’s Vite’s HMR at work!
Troubleshooting
- Blank page? Check browser console errors. Undo recent edits.
- Command not found? Reinstall Node or reopen the terminal.
- Network blocked? Corporate networks may block CDNs; try a different network or local install.
#next — What’s next
- Auth MFE blog #2: Implement the scope in 10 incremental examples—/login, /signup, /users list, navigation, and fake success/error via MSW.
- Auth MFE blog #3: Add beginner-friendly tests (no tests in this post).
Mindset: Build in tiny steps. Fake it first, add real auth later.
Comments
Post a Comment