Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 91004fe

Browse filesBrowse files
committed
增加第三方登录
1 parent 2287164 commit 91004fe
Copy full SHA for 91004fe

File tree

Expand file treeCollapse file tree

65 files changed

+13434
-12585
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

65 files changed

+13434
-12585
lines changed

‎backend/app/Events/GithubLogin.php

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class GithubLogin implements ShouldBroadcast
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @return void
21+
*/
22+
23+
public $user = '';
24+
public function __construct($user)
25+
{
26+
//
27+
$this->user = $user;
28+
$this->dontBroadcastToCurrentUser();
29+
}
30+
31+
/**
32+
* Get the channels the event should broadcast on.
33+
*
34+
* @return \Illuminate\Broadcasting\Channel|array
35+
*/
36+
public function broadcastOn()
37+
{
38+
return new Channel('github');
39+
}
40+
}
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class GithubLoginSuccess implements ShouldBroadcast
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @return void
21+
*/
22+
public $data = '';
23+
public function __construct($data)
24+
{
25+
//
26+
$this->data = $data;
27+
}
28+
29+
/**
30+
* Get the channels the event should broadcast on.
31+
*
32+
* @return \Illuminate\Broadcasting\Channel|array
33+
*/
34+
public function broadcastOn()
35+
{
36+
return new Channel('githubSuccess');
37+
}
38+
}

‎backend/app/Http/Controllers/Auth/LoginController.php

Copy file name to clipboardExpand all lines: backend/app/Http/Controllers/Auth/LoginController.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public function login()
6969
//$this->validateLogin(request());
7070
return $this->proxy->login(request('email'),request('password'));
7171
}
72+
public function loginWithThree()
73+
{
74+
//$this->validateLogin(request());
75+
return $this->proxy->loginWithThree(request('email'),request('password'), request('platformId'), request('provider'));
76+
}
7277

7378
/**
7479
* @api {post} /api/logout 注销用户登陆
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\User;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Auth;
8+
use Overtrue\LaravelSocialite\Socialite;
9+
use App\Models\ThreeLogin;
10+
11+
class AuthController extends Controller
12+
{
13+
/**
14+
* Redirect the user to the GitHub authentication page.
15+
*
16+
* @return Response
17+
*/
18+
19+
public function redirectToProvider(Request $request)
20+
{
21+
$request->session()->put('uuid', $request->input('time')) ;
22+
return Socialite::driver('github')->redirect();
23+
}
24+
25+
/**
26+
* Obtain the user information from GitHub.
27+
*
28+
* @return Response
29+
*/
30+
public function handleProviderCallback()
31+
{
32+
$user = Socialite::driver('github')->user()->toArray();
33+
$provider = $user['provider'];
34+
$id = $user['id'];
35+
// 查询返回的结果
36+
if (ThreeLogin::where('provider', $provider)->where('platform_id', $id)->first()){
37+
// 找到后登录
38+
$admin = ThreeLogin::where('provider', $provider)->where('platform_id', $id)->first();
39+
$admin = $admin->toArray();
40+
$id = $admin['user_id'];
41+
// 自动登录
42+
$userInstance = User::where('id', $id)->firstOrFail();
43+
Auth::login($userInstance);
44+
$token = $userInstance->createToken('token')->accessToken;
45+
// 得到$token
46+
$data['time'] = session('uuid') ;
47+
$data['token'] = $token;
48+
event(new \App\Events\GithubLoginSuccess($data));
49+
} else {
50+
// 没有找到
51+
52+
$user['time'] = session('uuid') ;
53+
event(new \App\Events\GithubLogin($user));
54+
}
55+
56+
// $user->token;
57+
}
58+
}
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\threeLogin;
6+
use Illuminate\Http\Request;
7+
8+
class ThreeLoginController extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
26+
{
27+
//
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @return \Illuminate\Http\Response
35+
*/
36+
public function store(Request $request)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Display the specified resource.
43+
*
44+
* @param \App\threeLogin $threeLogin
45+
* @return \Illuminate\Http\Response
46+
*/
47+
public function show(threeLogin $threeLogin)
48+
{
49+
//
50+
}
51+
52+
/**
53+
* Show the form for editing the specified resource.
54+
*
55+
* @param \App\threeLogin $threeLogin
56+
* @return \Illuminate\Http\Response
57+
*/
58+
public function edit(threeLogin $threeLogin)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Update the specified resource in storage.
65+
*
66+
* @param \Illuminate\Http\Request $request
67+
* @param \App\threeLogin $threeLogin
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function update(Request $request, threeLogin $threeLogin)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Remove the specified resource from storage.
77+
*
78+
* @param \App\threeLogin $threeLogin
79+
* @return \Illuminate\Http\Response
80+
*/
81+
public function destroy(threeLogin $threeLogin)
82+
{
83+
//
84+
}
85+
}

‎backend/app/Http/Proxy/TokenProxy.php

Copy file name to clipboardExpand all lines: backend/app/Http/Proxy/TokenProxy.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
namespace App\Http\Proxy;
4+
use App\Models\ThreeLogin;
5+
use Illuminate\Support\Facades\Auth;
46

57
/**
68
* Created by PhpStorm.
@@ -36,6 +38,23 @@ public function login($email, $password)
3638
'message' => 'Credentials not match'
3739
],421);
3840
}
41+
public function loginWithThree($email, $password, $id, $provider)
42+
{
43+
if (auth()->attempt(['email'=> $email, 'password'=> $password])){
44+
$user_id = Auth::user()->id;
45+
ThreeLogin::firstOrCreate(['platform_id'=>$id, 'provider'=>$provider, 'user_id' => $user_id]);
46+
return $this->proxy('password', [
47+
'username' => $email,
48+
'password' => $password,
49+
'scope' => '',
50+
]);
51+
}
52+
return response()->json([
53+
'status' => 'login error',
54+
'status_code' => 421,
55+
'message' => 'Credentials not match'
56+
],421);
57+
}
3958

4059
public function proxy($grantType, array $data = [])
4160
{

‎backend/app/Models/ThreeLogin.php

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class ThreeLogin extends Model
8+
{
9+
//
10+
protected $fillable = ['user_id', 'platform_id', 'remark', 'provider'];
11+
}

‎backend/composer.json

Copy file name to clipboardExpand all lines: backend/composer.json
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"laravel/passport": "^4.0",
1515
"laravel/tinker": "~1.0",
1616
"maatwebsite/excel": "~2.1.0",
17-
"overtrue/easy-sms": "^1.0"
17+
"overtrue/easy-sms": "^1.0",
18+
"overtrue/laravel-socialite": "~2.0",
19+
"pusher/pusher-php-server": "^3.0"
1820
},
1921
"require-dev": {
2022
"filp/whoops": "~2.0",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.