Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Auth;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Member;
- use Ramsey\Uuid\UuidFactory;
- use Validator;
- use Hash;
- use Illuminate\Support\Facades\Crypt as Crypt;
- use Illuminate\Support\Facades\DB;
- class AuthController extends Controller {
- protected $userid;
- function __construct() {
- $userid = $this->gen_userid();
- }
- public function page_register() {
- return view('auth.register');
- }
- public function post_account_register(Request $request) {
- $res = array(
- "error" => false,
- "email" => false,
- "username"=> false,
- "password" => false
- );
- $email = $request->input('email');
- $username = $request->input('username');
- $password = $request->input('password');
- $validator = Validator::make($request->all(), [
- 'email' => 'required|email|unique:members,email|max:255',
- 'username' => 'required|min:5|max:25|unique:members,username',
- 'password' => 'required|min:8|max:99'
- ]);
- if($validator->fails()) {
- $res["error"] = true;
- if($validator->errors()->has('email')) {
- $res["email"] = true;
- }
- if($validator->errors()->has('username')) {
- $res["username"] = true;
- }
- if($validator->errors()->has('password')) {
- $res["password"] = true;
- }
- } else {
- $d = array(
- "email" => $email,
- "username" => $username,
- "password" => $password
- );
- $this->create_account($d);
- $res = [
- "error" => false,
- "userid" => Crypt::encryptString($this->userid)
- ];
- return json_encode($res);
- }
- echo json_encode($res);
- }
- private function create_account($data) {
- $members = DB::table('members');
- if($members->where('username', $data["username"])->count() == 0 && $members->where("email",$data["email"])) {
- $members->insert(
- [
- 'email' => $data["email"],
- 'username' => $data["username"],
- 'password' => Hash::make($data["password"]),
- 'uuid' => $this->gen_uuid(),
- 'userid' => $this->userid
- ]
- );
- }
- }
- private function gen_uuid()
- {
- return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff), mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
- );
- }
- private function gen_userid() {
- $ret = "";
- $a = ["A","B","C","D","E","E","F","0","1","2","3","4","5","6","7","8","9"];
- for($i = 0; $i < 64; $i++) {
- $rand = rand(0,count($a));
- $ret .= $a[$rand];
- }
- return $ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement