Browse Source

create user and site on first login

pull/3/head
Aaron Parecki 9 years ago
parent
commit
aa1317aa0e
4 changed files with 50 additions and 2 deletions
  1. +38
    -1
      controllers/Auth.php
  2. +1
    -1
      controllers/Controller.php
  3. +10
    -0
      lib/helpers.php
  4. +1
    -0
      public/index.php

+ 38
- 1
controllers/Auth.php View File

@ -13,6 +13,17 @@ class Auth {
return $response; return $response;
} }
public function logout(Request $request, Response $response) {
session_start();
if(array_key_exists('user_id', $_SESSION)) {
$_SESSION['user_id'] = null;
session_destroy();
}
$response->setStatusCode(302);
$response->headers->set('Location', '/login');
return $response;
}
public function login_start(Request $request, Response $response) { public function login_start(Request $request, Response $response) {
if(!$request->get('url') || !($me = IndieAuth\Client::normalizeMeURL($request->get('url')))) { if(!$request->get('url') || !($me = IndieAuth\Client::normalizeMeURL($request->get('url')))) {
@ -102,9 +113,35 @@ class Auth {
} }
// Create or load the user // Create or load the user
$user = ORM::for_table('users')->where('url', $token['auth']['me'])->find_one();
if(!$user) {
$user = ORM::for_table('users')->create();
$user->url = $token['auth']['me'];
$user->created_at = date('Y-m-d H:i:s');
$user->last_login = date('Y-m-d H:i:s');
$user->save();
// Create a site for them with the default role
$site = ORM::for_table('sites')->create();
$site->name = 'My Website';
$site->created_by = $user->id;
$site->created_at = date('Y-m-d H:i:s');
$site->save();
$role = ORM::for_table('roles')->create();
$role->site_id = $site->id;
$role->user_id = $user->id;
$role->role = 'owner';
$role->token = random_string(32);
$role->save();
} else {
$user->last_login = date('Y-m-d H:i:s');
$user->save();
}
session_start(); session_start();
$_SESSION['me'] = $token['auth']['me'];
$_SESSION['user_id'] = $user->id;
$response->setStatusCode(302); $response->setStatusCode(302);
$response->headers->set('Location', ($state->return_to ?: '/dashboard')); $response->headers->set('Location', ($state->return_to ?: '/dashboard'));
return $response; return $response;

+ 1
- 1
controllers/Controller.php View File

@ -6,7 +6,7 @@ class Controller {
private function _is_logged_in(&$request, &$response) { private function _is_logged_in(&$request, &$response) {
session_start(); session_start();
if(!array_key_exists('me', $_SESSION)) {
if(!array_key_exists('user_id', $_SESSION)) {
session_destroy(); session_destroy();
$response->setStatusCode(302); $response->setStatusCode(302);
$response->headers->set('Location', '/login?return_to='.$request->getPathInfo()); $response->headers->set('Location', '/login?return_to='.$request->getPathInfo());

+ 10
- 0
lib/helpers.php View File

@ -24,3 +24,13 @@ function q() {
} }
return $caterpillar; return $caterpillar;
} }
function random_string($len) {
$charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$str = '';
$c = strlen($charset)-1;
for($i=0; $i<$len; $i++) {
$str .= $charset[mt_rand(0, $c)];
}
return $str;
}

+ 1
- 0
public/index.php View File

@ -15,6 +15,7 @@ $router->addRoute('POST', '/webmention', 'API::webmention');
$router->addRoute('GET', '/webmention/{code}', 'API::webmention_status'); $router->addRoute('GET', '/webmention/{code}', 'API::webmention_status');
$router->addRoute('GET', '/login', 'Auth::login'); $router->addRoute('GET', '/login', 'Auth::login');
$router->addRoute('GET', '/logout', 'Auth::logout');
$router->addRoute('POST', '/login/start', 'Auth::login_start'); $router->addRoute('POST', '/login/start', 'Auth::login_start');
$router->addRoute('GET', '/login/callback', 'Auth::login_callback'); $router->addRoute('GET', '/login/callback', 'Auth::login_callback');

Loading…
Cancel
Save