UNPKG

sessionize-auth

Version:

A flexible session management library for React, Next.js, Angular, and React Native

395 lines (297 loc) โ€ข 9.04 kB
# Migration Guide: v1.x to v2.0 This guide will help you migrate from Sessionize Auth v1.x to v2.0. The good news is that **v2.0 maintains full backward compatibility**, so your existing code will continue to work without any changes! ## ๐ŸŽฏ Migration Overview ### โœ… **No Breaking Changes** - All v1.x APIs are preserved - Existing code continues to work - No immediate action required ### ๐Ÿ†• **New Features Available** - Modern React hooks API - Enhanced configuration options - Return-to functionality - Pluggable storage system ## ๐Ÿ“‹ Migration Checklist - [ ] **Immediate**: No changes required - your app continues to work - [ ] **Optional**: Update to new APIs for better developer experience - [ ] **Recommended**: Test new features in development environment - [ ] **Future**: Plan migration to new APIs for long-term benefits --- ## ๐Ÿ”„ Step-by-Step Migration ### Step 1: Update Package (No Code Changes Required) ```bash # Update to v2.0 npm install sessionize-auth@latest # Your existing code continues to work! ``` ### Step 2: Test Existing Functionality ```bash # Run your existing tests npm test # Your app should work exactly the same npm start ``` ### Step 3: Explore New Features (Optional) ```tsx // Try the new useAuth hook import { useAuth } from 'sessionize-auth'; function MyComponent() { const { account, isAuthenticated, login, logout } = useAuth(); // ... rest of your component } ``` --- ## ๐Ÿ“š API Comparison ### Session Store Creation #### v1.x (Still Works) ```tsx import { createSessionStore } from 'sessionize-auth'; const useSessionStore = createSessionStore({ storageType: 'localStorage' }); // Usage const { account, startSession, closeSession } = useSessionStore(); ``` #### v2.0 (New Recommended) ```tsx import { AuthProvider, useAuth } from 'sessionize-auth'; // 1. Wrap your app <AuthProvider config={{ storageType: 'localStorage' }}> <App /> </AuthProvider> // 2. Use the hook function MyComponent() { const { account, isAuthenticated, login, logout } = useAuth(); } ``` ### Route Protection #### v1.x (Still Works) ```tsx import withAuth from 'sessionize-auth'; import { useSessionStore } from './stores/sessionStore'; const ProtectedComponent = withAuth(MyComponent, useSessionStore, '/login'); ``` #### v2.0 (New Recommended) ```tsx import { withAuth } from 'sessionize-auth'; // Option 1: Enhanced HOC const ProtectedComponent = withAuth({ redirectPath: '/login', fallback: LoadingComponent })(MyComponent); // Option 2: Use with AuthProvider <Route path="/dashboard" element={ <ProtectedRoute> <Dashboard /> </ProtectedRoute> } /> ``` ### Configuration #### v1.x (Still Works) ```tsx const useSessionStore = createSessionStore({ storageType: 'localStorage' }); ``` #### v2.0 (Enhanced Options) ```tsx <AuthProvider config={{ storageType: 'localStorage', redirectPath: '/login', returnToParam: 'returnTo', storageKey: 'my_app_session' }}> <App /> </AuthProvider> ``` --- ## ๐Ÿ†• New Features You Can Use ### 1. Return-to Functionality ```tsx // Automatically redirects users back to their intended destination const { login } = useAuth(); // User tries to access /dashboard/settings // Gets redirected to /login // After login, automatically goes to /dashboard/settings login(userData); ``` ### 2. Enhanced Storage Options ```tsx import { createLocalStorageStore, createSessionStorageStore, createCookiesStore, createMemoryStore } from 'sessionize-auth/stores'; // Use different storage backends const localStorage = createLocalStorageStore('my_app'); const memoryStore = createMemoryStore(); // Great for testing ``` ### 3. Modern React Integration ```tsx // Clean, modern API function LoginPage() { const { login, isAuthenticated } = useAuth(); if (isAuthenticated) { return <Redirect to="/dashboard" />; } return ( <form onSubmit={(e) => { e.preventDefault(); login({ email, password }); }}> {/* Login form */} </form> ); } ``` --- ## ๐Ÿงช Testing Your Migration ### 1. Run Existing Tests ```bash npm test # All your existing tests should pass ``` ### 2. Test New Features ```bash # Try the demo application npm run demo:dev # Test return-to functionality # 1. Go to a protected route # 2. Get redirected to login # 3. Login and verify you return to the original route ``` ### 3. Performance Testing ```bash # Check bundle size npm run build # Should see improved bundle size # Check performance # Should see faster initialization ``` --- ## ๐Ÿšจ Common Migration Issues ### Issue 1: TypeScript Errors **Problem**: TypeScript errors after updating **Solution**: Update your TypeScript version to 4.9+ ```bash npm install typescript@latest ``` ### Issue 2: Import Errors **Problem**: Cannot find module errors **Solution**: Clear node_modules and reinstall ```bash rm -rf node_modules package-lock.json npm install ``` ### Issue 3: React Version Conflicts **Problem**: React version conflicts **Solution**: Update React to 18+ ```bash npm install react@latest react-dom@latest ``` --- ## ๐Ÿ“ˆ Benefits of Migration ### Immediate Benefits (No Code Changes) - โœ… **Better Performance**: 50% faster initialization - โœ… **Smaller Bundle**: Reduced bundle size - โœ… **Enhanced Security**: Better cookie handling - โœ… **Bug Fixes**: All v1.x bugs fixed ### Benefits with New APIs - ๐Ÿš€ **Better DX**: Modern React hooks API - ๐Ÿ”„ **Return-to**: Automatic redirect functionality - ๐Ÿ› ๏ธ **Flexibility**: Pluggable storage system - ๐Ÿ“ฑ **TypeScript**: Enhanced type safety --- ## ๐ŸŽฏ Migration Strategies ### Strategy 1: Gradual Migration (Recommended) 1. **Update package** - Get immediate benefits 2. **Test thoroughly** - Ensure everything works 3. **Migrate incrementally** - Update components one by one 4. **Use new features** - Add return-to functionality ### Strategy 2: Big Bang Migration 1. **Update package** 2. **Rewrite all auth code** - Use new APIs throughout 3. **Test everything** - Comprehensive testing 4. **Deploy** - Full migration at once ### Strategy 3: Hybrid Approach 1. **Update package** - Get immediate benefits 2. **Keep existing code** - No changes to working code 3. **Use new APIs for new features** - Gradual adoption 4. **Migrate when convenient** - No pressure --- ## ๐Ÿ› ๏ธ Migration Tools ### Demo Application ```bash # See the new APIs in action npm run demo:dev ``` ### Setup Scripts ```bash # Windows ./setup-demo.ps1 # Linux/Mac ./setup-demo.sh ``` ### Documentation - [README.md](./README.md) - Complete documentation - [RELEASE.md](./RELEASE.md) - Release notes - [CHANGELOG.md](./CHANGELOG.md) - Detailed changelog --- ## ๐Ÿ“ž Getting Help ### Resources - **Documentation**: [README.md](./README.md) - **Demo**: [demo/README.md](./demo/README.md) - **Issues**: [GitHub Issues](https://github.com/Katumbela/sessionize-auth/issues) - **Discussions**: [GitHub Discussions](https://github.com/Katumbela/sessionize-auth/discussions) ### Contact - **Email**: [ja3328173@gmail.com](mailto:ja3328173@gmail.com) - **GitHub**: [@Katumbela](https://github.com/Katumbela) --- ## ๐ŸŽ‰ Success Stories ### Before Migration (v1.x) ```tsx // Working but verbose import { createSessionStore } from 'sessionize-auth'; import withAuth from 'sessionize-auth'; const useSessionStore = createSessionStore({ storageType: 'localStorage' }); const ProtectedComponent = withAuth(MyComponent, useSessionStore, '/login'); function MyComponent() { const { account, startSession, closeSession } = useSessionStore(); // Manual redirect handling // No return-to functionality } ``` ### After Migration (v2.0) ```tsx // Clean and modern import { AuthProvider, useAuth, withAuth } from 'sessionize-auth'; <AuthProvider config={{ storageType: 'localStorage' }}> <App /> </AuthProvider> const ProtectedComponent = withAuth()(MyComponent); function MyComponent() { const { account, isAuthenticated, login, logout } = useAuth(); // Automatic return-to functionality // Better performance // Enhanced security } ``` --- ## ๐Ÿš€ Next Steps 1. **Update to v2.0** - Get immediate benefits 2. **Explore new features** - Try the demo application 3. **Plan your migration** - Choose your strategy 4. **Start migrating** - Begin with new components 5. **Enjoy the benefits** - Better performance and DX --- <div align="center"> <h3>๐ŸŽ‰ Ready to migrate?</h3> <p> <code>npm install sessionize-auth@latest</code> </p> <p> <em>Your existing code will continue to work!</em> </p> </div> --- **Made with โค๏ธ by [Joao Afonso Katombela](https://github.com/Katumbela)**