reinvanoyen/dry-accounts

There is no license information available for the latest version (1.0.7) of this package.

Account system for DRY applications

1.0.7 2020-07-08 09:10 UTC

This package is auto-updated.

Last update: 2024-11-08 18:52:45 UTC


README

Account system for DRY applications

Installation

composer require reinvanoyen/dry-accounts

php oak migration migrate -m accounts
Config options

Basic example usage

Usage
<?php

namespace controller;

class authentication
{
    public static function login(Request $request)
    {
        $login_form = new Form( $request);
        
        $login_form->add_email( 'email', [ 'required' => true ] );
        $login_form->add_password( 'password', [ 'required' => true ] );

        $auth_failed = false;
        $is_logged_in = \Tnt\Account\Facade\Auth::isAuthenticated();

        if( $login_form->validate() ) {

            if( \Tnt\Account\Facade\Auth::authenticate( $login_form->get( 'email' ), $login_form->get( 'password' ) ) ) {
                $is_logged_in = true;

            } else {
                $auth_failed = true;
            }
        }

        $tpl = new Template();
        $tpl->login_form = $login_form;
        $tpl->auth_failed = $auth_failed;
        $tpl->is_logged_in = $is_logged_in;
        $tpl->render('users/login.tpl');
    }
    
    public static function register(Request $request)
    {
        $app = Application::get();

        $register_form = new Form( $request);

        $register_failed = false;

        $register_form->add_email('email', [
            'required' => true,
            'extra_validation' => function( $value, &$errors ) use ( &$register_failed, $app )
            {
                if ($app->get(AuthenticationInterface::class)->getActivatedUser($value)) {

                    $errors[] = 'user_already_activated';
                    $register_failed = true;
                }
            }
        ]);

        $register_form->add_password( 'password', ['required' => true,] );

        if( $register_form->validate() ) {

            $user = $app->get(AuthenticationInterface::class)
                ->register(
                    $register_form->get('email'),
                    $register_form->get('password')
                );

            echo 'User registered ' . $user->email;
        }

        $tpl = new Template();
        $tpl->register_form = $register_form;
        $tpl->register_failed = $register_failed;
        $tpl->render('users/register.tpl');
    }

    public static function logout(Request $request)
    {
        \Tnt\Account\Facade\Auth::logout();

        Response::redirect('login/');
    }
}