@disruptph/react-auth-context
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

@disruptph/react-auth-context

Generic react auth context

Usage

Typescript/React Example

import React, { useState } from 'react';
import { AuthProvider, AuthService, useAuth } from '@disruptph/react-auth-context';

class MyAuthService implements AuthService {
  async loginWithEmail(email: string, password: string) {
    // Handle email/password login via custom backend api, firebase, or anything you prefer
  }

  async checkAuth() {
    // Handle auth check
  }

  async logout() {
    // Handle logout
  }
}

const App = () => (
  <AuthProvider service={new MyAuthService()}>
    <Login />
  </AuthProvider>
);

const Login = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const { login } = useAuth();

  const handleSubmit = async () => {
    await login(email, password);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input value={email} onChange={e => setEmail(e.target.value)} />
      <input value={password} onChange={e => setPassword(e.target.value)} type="password" />
    </form>
  );
};

Readme

Keywords

Package Sidebar

Install

npm i @disruptph/react-auth-context

Weekly Downloads

16

Version

2.0.1

License

MIT

Unpacked Size

11 kB

Total Files

7

Last publish

Collaborators

  • onoya
  • calimbas