laravel的passport的登录/注册/获取详情

1、登录

public function login(){

    if(Auth::attempt(array('name' => request('name'), 'password' => request('password')))){

        $user = Auth::user();

        $success['token'] =  $user->createToken('MyApp')->accessToken;

        return response()->json(['success' => $success], $this->successStatus);

    }

    else{

        return response()->json(['error'=>'Unauthorised'], 401);

    }

}



2、注册

public function register(Request $request)

{

    $validator = Validator::make($request->all(), [

        'name' => 'required|min:2|max:20',

        'password' => 'required',

        'c_password' => 'required|same:password',

    ],['name.required'=>'名字不能为空',

        'name.min'=>'名字最少2个字',

        'name.max'=>'名字最长20个字',

        'password.required'=>'密码不能为空',

        'c_password.required'=>'请再次输入密码',

        'c_password.same'=>'密码不一致,请重新输入']);

    if ($validator->fails()) {

        return response()->json(['error'=>$validator->errors()], 401);

    }

    $input = $request->all();

    $input['password'] = bcrypt($input['password']);

    $user = User::create($input);

    $success['token'] =  $user->createToken('MyApp')->accessToken;

    $success['name'] =  $user->name;

    return response()->json(['success'=>$success], $this->successStatus);

}

3、获取详情

public function getDetails()

{

    $user = Auth::user();

    return response()->json(['success' => $user], $this->successStatus);

}

你可能感兴趣的:(laravel的passport的登录/注册/获取详情)