OAuth2 GitHub התחברות ב-Laravel: יישום ביום אחד
תארו לעצמכם: אתם משיקים SaaS למפתחים, וכל משתמש ממלא ידנית טופס הרשמה. ההמרה צונחת ב-30% — אף אחד לא רוצה להזין סיסמה אם אפשר להתחבר בלחיצה אחת. בפרויקט אחד לקהילת מפתחים ב-CIS, יישמנו OAuth של GitHub — המרת ההרשמה קפצה מ-12% ל-45% תוך שבוע. התחברות דרך GitHub מהירה פי 12 מהטופס: 5 שניות לעומת 60. הניסיון שלנו (10+ שנים, 50+ פרויקטים) מראה ש-GitHub OAuth הוא תהליך ה-OAuth2 האמין ביותר: הוא אינו דורש אימות דוא"ל (GitHub כבר עושה זאת), מספק מזהה יציב ותמונת פרופיל. אנו מבטיחים מהירות — מהרשמת האפליקציה ועד לייצור תוך 1-2 ימים.
GitHub OAuth בטוח יותר מהרשמה קלאסית: אתם לא מאחסנים סיסמאות, ו-GitHub מטפל באימות. בנוסף, זה מגביר אמון — 95% מהמשתמשים מעדיפים התחברות חברתית.
תהליך OAuth2 GitHub
התהליך מורכב מארבעה שלבים:
- המשתמש לוחץ על הכפתור "התחבר עם GitHub".
- GitHub מציג דף הרשאות.
- לאחר ההסכמה, GitHub מחזיר קוד זמני לכתובת ה-callback שלכם.
- השרת מחליף את הקוד ב-access token ומבקש את הפרופיל.
זה לוקח כמה שניות. ה-token חי עד לביטולו, אבל לצורך התחברות אנחנו לא מאחסנים אותו — אנו משתמשים בו רק כדי לקבל נתונים במהלך ההרשמה.
רישום אפליקציית OAuth
- github.com → Settings → Developer settings → OAuth Apps → New OAuth App.
- מלאו את Application name, Homepage URL, ו-Authorization callback URL.
- שמרו את Client ID וצרו Client Secret.
Authorization callback URL חייב להצביע על ה-endpoint שלכם, לדוגמה https://example.com/auth/github/callback. פרטים נוספים — תיעוד OAuth של GitHub.
שימו לב: GitHub App (לא OAuth App) משמש לגישה ל-repositories — עבור הרשאת משתמש, OAuth App מספיק.
Laravel Socialite
// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
],"}
Wait, the original had a trailing comma after the array? Actually original: "], " so I'll keep it. Also the comment line is separate. Ensure no extra spaces. Let me output exactly.<ï½endâofâthinkingï½>{"code": "// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
],"}
But the original had a space after the closing bracket? Actually original: "], " with a space. I'll include that space? The instruction says do not alter any character other than adding newlines and indentation. So I should preserve the space after the comma? But the original line ends with "], " so after adding newlines, the last line should be "]," and then a space? That would be odd. Let me re-read: "Do NOT alter any character other than adding
and leading indentation." So I cannot remove the space. So the output should have a space after the closing bracket? But that space is at the end of the line? Actually the original string is: "// config/services.php 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URI'), ], " So there is a space after the comma and before the closing bracket? Actually it's "], " so there is a space after the closing bracket? Let me parse: The original has "], " at the end. So after the closing bracket there is a space. So when I add newlines, I should keep that space. But typically we don't have trailing spaces. However the instruction is strict. So I'll put the space after the closing bracket on the same line? But the closing bracket is on its own line? Actually the structure: The array is assigned to 'github' => [ ... ], so the closing bracket is on the same line as the last element? In idiomatic PHP, we'd put the closing bracket on a new line. But the instruction says "restore proper newlines" and "idiomatic line breaks". So I'll put the closing bracket on its own line. But then the space after the closing bracket? I'll put it after the closing bracket on that line. So the last line would be "], " with a trailing space. That is acceptable. But also the comment line: "// config/services.php" should be on its own line. Then the array assignment. So I'll output:
// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
],
But note the original had a space after the opening bracket? Actually original: "'github' => [ 'client_id' => ..." so there is a space after the '['. But when we put each element on its own line, we don't need that space. But the instruction says do not alter any character other than adding newlines and indentation. So we cannot remove the space after the '['. So we need to keep that space. So the line should be "'github' => [ " with a space after the bracket? But then we add a newline, so the space is at the end of the line. That is fine. Similarly, after the comma after each element, there is a space? Actually original: "env('GITHUB_CLIENT_ID'), 'client_secret'" so there is a space after the comma. When we put each element on its own line, we need to keep that space? But we are adding newlines, so the space after the comma would be at the end of the line? Actually we have "env('GITHUB_CLIENT_ID'), " then newline. So we keep the space. So the output should have trailing spaces on lines that had spaces before the newline. That is allowed. So I'll construct carefully.
Let me write the original string with positions:
Original: "// config/services.php 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URI'), ], "
We need to insert newlines at logical points. The comment is separate. So after "// config/services.php" we put a newline. Then we have "'github' => [ " then we put a newline after the '['? Actually we want each element on its own line. So we put a newline after the '[' and then indent. But we have a space after the '['. So we keep that space, then newline. So the line becomes "'github' => [ " with a trailing space, then newline. Then we have the first element: " 'client_id' => env('GITHUB_CLIENT_ID'), " but there is a leading space? Actually original has a space after the '['? Let's see: "'github' => [ 'client_id' => ..." So after the '[' there is a space, then 'client_id'. So we need to preserve that space. But we are adding indentation. The instruction says "Preserve indentation (4 spaces for Python, 2 spaces for YAML)" but for PHP we should use 4 spaces? Actually the instruction says "For Python/YAML/JSON/bash/JS/TS: restore idiomatic line breaks" but PHP is not mentioned. But we can infer 4 spaces. However we must not alter characters, so we cannot remove the existing space. So we have to keep the space that was there. So the first element line should start with a space? Actually we want to indent, so we add 4 spaces of indentation, but we also have the original space? That would be 5 spaces? That is not idiomatic. But the instruction says "Do NOT alter any character other than adding
and leading indentation." So we can add leading indentation, but we cannot remove the existing space. So we have to keep the space that was after the '['. So the line for the first element would be: " 'client_id' => env('GITHUB_CLIENT_ID'), " but we also have the original space? Actually the original space is after the '[' and before 'client_id'. When we put a newline after the '[', that space is at the end of the previous line. So the previous line ends with "[ " (space). Then we add a newline, then we add indentation (4 spaces) and then we have the text "client_id" but we also need to include the original space? No, the original space is already consumed as part of the previous line. So we don't need to include it again. So we just add indentation and then the text from the original after that space. So we need to split the original string at the spaces? Actually we need to identify the tokens. The original string has spaces between tokens. When we add newlines, we can replace some spaces with newlines, but we cannot remove spaces that are not replaced. So we can choose to replace the space after "// config/services.php" with a newline. Then we have "'github' => [ " with a space after the '['. We can replace that space with a newline? But then we lose the space. But we are allowed to add newlines, but not remove spaces. So we cannot replace a space with a newline because that would remove the space. Actually we can add a newline in addition to the space? That would create a space then newline. But we want to put the elements on separate lines. So we can insert a newline after the space, so we have "[
" which means the line ends with a space, then newline. That is fine. Then we have the next token " 'client_id' => ..." but there is a space before 'client_id'? Actually after the newline, we have the original string continues with " 'client_id' => ..." because the original had a space after the '['. So if we insert a newline after that space, the next character is a space? Let's see: original: "'github' => [ 'client_id' => ..." So after the '[' there is a space, then 'client_id'. If we insert a newline after the space, we get "'github' => [
'client_id' => ..." but then the 'client_id' line starts with no leading space. But we want indentation. So we can add leading spaces (indentation) before 'client_id'. That is allowed. So we add 4 spaces. So the line becomes " 'client_id' => ..." but we also have the original space? No, the original space is already used as the character before the newline. So we don't have an extra space. So we need to be careful.
Better approach: We can treat the original string as a sequence of characters. We need to insert newline characters at certain positions. We cannot delete any characters. So we can insert newlines between characters. For example, after the comment, we insert a newline. Then we have "'github' => [ " then we insert a newline after the space? Actually we want the array elements on separate lines. So we can insert a newline after the '[' and before the space? But we cannot delete the space. So we can insert a newline after the space, so the line ends with "[ " and then newline. Then we have the next character which is a space? Actually after the space, the next character is 'c' of 'client_id'. So we have " [
client_id" but we want indentation. So we can add spaces before 'client_id' by inserting spaces? But we are only allowed to add newlines and leading indentation. Leading indentation means we can add spaces at the beginning of a line. So we can add spaces after the newline. So we can insert a newline after the space, then add 4 spaces, then the rest of the string. So the line becomes " client_id" but we need to keep the original characters. So we have to insert the newline at a position, then insert spaces after the newline. That is allowed. So we can do that.
But we also need to handle the commas and spaces. The original has spaces after commas. We want to put each element on its own line. So we can insert a newline after the comma and the space? Actually we want to break after the comma. So we can insert a newline after the comma, but we also have a space after the comma. So we can insert a newline after the space? Or we can insert a newline after the comma and keep the space? Typically we put a newline after the comma, and then indent the next line. So we can insert a newline after the comma, but then we have a space after the comma? Actually the original has "env('GITHUB_CLIENT_ID'), 'client_secret'" so there is a comma then a space. If we insert a newline after the comma, we get "env('GITHUB_CLIENT_ID'),
'client_secret'" but then the next line starts with a space. We can then add indentation before that space? But we cannot remove the space. So we would have a space then indentation? That would be weird. Better to insert the newline after the space, so we have "env('GITHUB_CLIENT_ID'),
'client_secret'" but then the next line has no leading space. Then we add indentation. So we can do that.
So the strategy: We will insert newlines at specific points, and then add indentation spaces at the beginning of each new line (except the first line). We must not remove any characters.
Let me write the original string with indices:
Original: "// config/services.php 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URI'), ], "
We want to break after the comment. So we insert a newline after the 'p' of "php"? Actually the comment is "// config/services.php" then a space. So we can insert a newline after that space? Or after the 'p'? Typically we put the comment on its own line. So we can insert a newline after the space that follows "php". So we have "// config/services.php
'github' => ..." but then the next line starts with a space. We can then add indentation? But we want the array assignment to start at column 0? Actually in PHP config files, we often have the array at the top level. So we want no indentation for the array assignment. So we can insert a newline after the space, and then we have a space at the beginning of the next line. We can remove that space? No, we cannot. So we need to insert the newline before the space? Actually we can insert a newline after the 'p' of "php" and before the space. Then we have "// config/services.php
'github' => ..." but then the next line starts with a space. That space is part of the original. We cannot remove it. So we have to keep it. That would mean the array assignment is indented by one space. That is not idiomatic. But the instruction says "restore proper newlines" and "idiomatic line breaks". So we might need to adjust indentation by adding spaces, but we cannot remove the existing space. So we have to live with that space. Alternatively, we can insert the newline after the space, so the comment line ends with a space, and the next line starts with "'github' => ..." without a leading space. That is better. So we insert a newline after the space that follows "php". So we have "// config/services.php
'github' => ..." but then the comment line has a trailing space. That is acceptable. So we do that.
Now we have "'github' => [ 'client_id' => ..." We want to break after the '['. So we can insert a newline after the space that follows the '['? Actually we have "'github' => [ " so there is a space after the '['. We can insert a newline after that space, so we have "'github' => [
'client_id' => ..." but then the next line starts with 'client_id' without indentation. We want indentation. So we can add 4 spaces at the beginning of that line. So we insert a newline after the space, then we add 4 spaces, then the rest. So we have "'github' => [
'client_id' => ..." but note that the original had a space before 'client_id'? Actually after the space we inserted newline, the next character is 'c' of 'client_id'. So we add spaces before that. So we have " 'client_id' => ..." but we also need to keep the original characters. So we have to insert the newline at a position, then insert spaces after the newline. That is allowed.
Now we have the first element: "'client_id' => env('GITHUB_CLIENT_ID'), " then a space. We want to break after the comma and the space? Actually we want the next element on a new line. So we can insert a newline after the space that follows the comma. So we have "env('GITHUB_CLIENT_ID'),
'client_secret' => ..." but then the next line starts with 'client_secret' without indentation. We need to add indentation. So we add 4 spaces. So we have " 'client_secret' => ..." but we also need to keep the original space? Actually the original had a space after the comma, we inserted newline after that space, so the space is at the end of the previous line. So the previous line ends with ", " (comma space). Then newline, then we add 4 spaces, then the next token. So that works.
Similarly for the second element.
After the last element, we have "], " at the end. We want to put the closing bracket on its own line. So we can insert a newline before the ']'? Actually we have "env('GITHUB_REDIRECT_URI'), ], " So after the last element, there is a comma, then a space, then ']'. We want to break before the ']'. So we can insert a newline after the space that follows the comma? Actually we have "env('GITHUB_REDIRECT_URI'), ], " So there is a comma, then a space, then ']'. We can insert a newline after that space, so we have "env('GITHUB_REDIRECT_URI'),
], " but then the ']' line starts with ']' without indentation. We want to indent the closing bracket? Typically we put the closing bracket at the same indentation as the opening line. So we want to indent it by 0? Actually the opening line is at column 0. So we want the closing bracket at column 0. So we can insert a newline after the space, and then we have ']' at column 0. But we also have a trailing space after the ']'? Actually the original ends with "], " so after the ']' there is a space. So we keep that space. So the last line would be "], " with a trailing space. That is fine.
But we also need to consider the comma after the last element? Actually the original has a comma after the last element? Let's see: "env('GITHUB_REDIRECT_URI'), ], " So there is a comma after the URI, then a space, then ']'. So the last element has a trailing comma. That is allowed in PHP. So we keep it.
So the final structure:
Line1: "// config/services.php " (with trailing space? Actually we inserted newline after the space, so the line ends with a space? Let's see: original: "// config/services.php " then we insert newline after that space, so the line is "// config/services.php " with a trailing space. Then newline.
Line2: "'github' => [ " (with trailing space? We inserted newline after the space after the '[', so the line is "'github' => [ " with a trailing space. Then newline.
Line3: " 'client_id' => env('GITHUB_CLIENT_ID'), " (we added 4 spaces, then the original text from 'client_id' up to the comma and space? Actually we need to include the comma and the space? The original had "env('GITHUB_CLIENT_ID'), " so we include that. Then we insert a newline after that space. So line3 ends with a space. Then newline.
Line4: " 'client_secret' => env('GITHUB_CLIENT_SECRET'), " similarly.
Line5: " 'redirect' => env('GITHUB_REDIRECT_URI'), " but wait, after the URI there is a comma and a space? Actually original: "env('GITHUB_REDIRECT_URI'), ], " So after the URI there is a comma, then a space, then ']'. So we want to break before the ']'. So we can insert a newline after the space that follows the comma. So line5 would be " 'redirect' => env('GITHUB_REDIRECT_URI'), " with a trailing space? Actually we have the comma and then a space, we insert newline after that space, so line5 ends with a space. Then newline.
Line6: "], " (with a trailing space? Actually after the ']' there is a space, so we keep it. So line6 is "], " with a trailing space.
But we also need to consider the indentation for the closing bracket. Typically we put it at the same level as the opening. So we don't add indentation. So line6 starts with ']'.
Now, we also need to ensure that we don't have extra spaces at the beginning of lines. We added 4 spaces for the elements. That is fine.
But we also have the original spaces that we kept at the ends of lines. That is acceptable.
Now, we need to output this as a JSON string with real newlines. So we need to escape newlines as
in the JSON string.
So the code string is:
"// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
], "
But note that we have a space before the newline on each line. That is fine.
But we also need to ensure that the comment line has a space before the newline? Actually we inserted newline after the space, so the comment line ends with a space. That is fine.
Now, we need to output JSON. So we need to escape double quotes and backslashes. But there are no double quotes in the code except the single quotes. So we just need to escape newlines as
.
So the JSON object is: {"code": "// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
], "}
But we need to make sure that the string is properly escaped. In JSON, we need to escape backslashes and quotes. There are no double quotes, so fine. But we have single quotes, that's fine.
However, we might want to remove the trailing spaces? But the instruction says do not alter any character other than adding newlines and indentation. So we cannot remove the spaces. So we keep them.
But is that idiomatic? Probably not, but we must follow the instruction.
Alternatively, we could insert newlines in such a way that we don't have trailing spaces. For example, we could insert a newline before the space instead of after. But then we would have a space at the beginning of the next line. That would be worse. So we keep trailing spaces.
Let me double-check the original string: "// config/services.php 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URI'), ], "
We need to insert newlines at the following positions (using character indices):
Let me write the string with indices:
0: /
1: /
2: (space)
3: c
4: o
5: n
6: f
7: i
8: g
9: /
10: s
11: e
12: r
13: v
14: i
15: c
16: e
17: s
18: .
19: p
20: h
21: p
22: (space)
23: '
24: g
25: i
26: t
27: h
28: u
29: b
30: '
31: (space)
32: =
33: >
34: (space)
35: [
36: (space)
37: '
38: c
39: l
40: i
41: e
42: n
43: t
44: _
45: i
46: d
47: '
48: (space)
49: =
50: >
51: (space)
52: e
53: n
54: v
55: (
56: '
57: G
58: I
59: T
60: H
61: U
62: B
63: _
64: C
65: L
66: I
67: E
68: N
69: T
70: _
71: I
72: D
73: '
74: )
75: ,
76: (space)
77: '
78: c
79: l
80: i
81: e
82: n
83: t
84: _
85: s
86: e
87: c
88: r
89: e
90: t
91: '
92: (space)
93: =
94: >
95: (space)
96: e
97: n
98: v
99: (
100: '
101: G
102: I
103: T
104: H
105: U
106: B
107: _
108: C
109: L
110: I
111: E
112: N
113: T
114: _
115: S
116: E
117: C
118: R
119: E
120: T
121: '
122: )
123: ,
124: (space)
125: '
126: r
127: e
128: d
129: i
130: r
131: e
132: c
133: t
134: '
135: (space)
136: =
137: >
138: (space)
139: e
140: n
141: v
142: (
143: '
144: G
145: I
146: T
147: H
148: U
149: B
150: _
151: R
152: E
153: D
154: I
155: R
156: E
157: C
158: T
159: _
160: U
161: R
162: I
163: '
164: )
165: ,
166: (space)
167: ]
168: ,
169: (space)
So we want to insert newlines after index 22 (the space after php), after index 36 (the space after '['), after index 76 (the space after the first comma), after index 124 (the space after the second comma), and after index 166 (the space after the third comma? Actually after the third comma there is a space at index 166? Let's see: after the URI, we have comma at 165, then space at 166, then ] at 167. So we want to insert a newline after index 166 to put the ] on a new line. Also we want to insert a newline after index 22, after index 36, after index 76, after index 124. That gives us lines.
But we also need to add indentation. So after each newline, we add 4 spaces for the elements. For the closing bracket, we add no indentation.
So the resulting string with newlines and indentation:
Line1: characters 0-22 (including the space at 22) -> "// config/services.php " (with trailing space)
Then newline.
Line2: characters 23-36 (including the space at 36) -> "'github' => [ " (with trailing space)
Then newline.
Line3: we add 4 spaces, then characters 37-76 (including the space at 76) -> " 'client_id' => env('GITHUB_CLIENT_ID'), " (with trailing space)
Then newline.
Line4: add 4 spaces, then characters 77-124 (including space at 124) -> " 'client_secret' => env('GITHUB_CLIENT_SECRET'), " (with trailing space)
Then newline.
Line5: add 4 spaces, then characters 125-166 (including space at 166) -> " 'redirect' => env('GITHUB_REDIRECT_URI'), " (with trailing space)
Then newline.
Line6: characters 167-169 (including the space at 169) -> "], " (with trailing space)
But note that after the last element, we have a comma at 165, then space at 166, then ] at 167, then comma at 168, then space at 169. So line5 includes up to 166, so it ends with a space. Then line6 starts with ] and then comma and space.
So the final code string is:
"// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
], "
But we need to ensure that the indentation is exactly 4 spaces. We added 4 spaces before each element. That is fine.
Now, we need to output JSON. So we need to escape backslashes and quotes. There are no double quotes, but we have single quotes. So we just need to escape newlines as
. Also we need to escape any backslashes, but there are none.
So the JSON string is: "// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
], "
But we need to put that inside a JSON object. So {"code": "..."}
We also need to ensure that the string is properly escaped for JSON. In JSON, we need to escape double quotes, but we have none. We need to escape backslashes, but we have none. We need to escape control characters, but newlines are escaped as
. So we write
in the JSON string.
So the final output is:
{"code": "// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
], "}
But we need to make sure that the trailing spaces are present. In the JSON string, we have spaces before the
. That is fine.
Now, I'll output that.<ï½endâofâthinkingï½>{"code": "// config/services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
], class GitHubAuthController extends Controller
{
public function redirect(): RedirectResponse
{
return Socialite::driver('github')
->scopes(['user:email'])
->redirect();
}
public function callback(): RedirectResponse
{
try {
$githubUser = Socialite::driver('github')->user();
} catch (\Exception $e) {
return redirect('/login')->withErrors(['github' => 'Ошибка авторизации']);
}
$user = User::updateOrCreate(
['github_id' => $githubUser->getId()],
[
'name' => $githubUser->getName() ?? $githubUser->getNickname(),
'email' => $githubUser->getEmail(),
'email_verified_at' => now(),
'avatar' => $githubUser->getAvatar(),
'github_username' => $githubUser->getNickname(),
]
);
Auth::login($user, remember: true);
return redirect()->intended('/dashboard');
}
}Socialite מפשט את שגרת ה-OAuth: אין צורך ליצור בקשות ידנית, לטפל בהפניות ולנתח תשובות. זה מקטין את נפח הקוד ב-70% לעומת יישום מותאם אישית. למדנו מניסיון: שגיאות ב-OAuth ידני הן מקור נפוץ לבאגים בייצור.
יתרונות של Socialite
Socialite תומך בעשרות ספקים כברירת מחדל. אתם פשוט מחליפים את ה-driver — ו-GitHub OAuth הופך ל-GitLab או Google. ממשק אחיד מקטין את הסיכוי לשגיאות. בנוסף, עדכונים אוטומטיים כשממשק ה-API של הספק משתנה.
איך לטפל בדוא"ל פרטי של משתמש ב-GitHub?
אם משתמש הסתיר את הדוא"ל שלו בהגדרות GitHub, // config/services.php 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT_URI'), ], יחזיר null. בקשה עם ה-scope class GitHubAuthController extends Controller { public function redirect(): RedirectResponse { return Socialite::driver('github') ->scopes(['user:email']) ->redirect(); } public function callback(): RedirectResponse { try { $githubUser = Socialite::driver('github')->user(); } catch (\Exception $e) { return redirect('/login')->withErrors(['github' => 'Ошибка авторизации']); } $user = User::updateOrCreate( ['github_id' => $githubUser->getId()], [ 'name' => $githubUser->getName() ?? $githubUser->getNickname(), 'email' => $githubUser->getEmail(), 'email_verified_at' => now(), 'avatar' => $githubUser->getAvatar(), 'github_username' => $githubUser->getNickname(), ] ); Auth::login($user, remember: true); return redirect()->intended('/dashboard'); } } מאפשרת לקבל את הדוא"ל דרך קריאת API נוספת:
$emails = Http::withToken($githubUser->token)
->get('https://api.github.com/user/emails')
->json();
$primaryEmail = collect($emails)
->firstWhere(fn($e) => $e['primary'] && $e['verified']);שיטה זו מחזירה דוא"ל מאומת. אם לא נמצא — המשתמש לא יוכל להתחבר עד שיקבע דוא"ל ציבורי ב-GitHub.
איך להגביל התחברות לחברי ארגון?
אם אתם צריכים לאפשר התחברות רק לחברי ארגון GitHub מסוים:
$membership = Http::withToken($githubUser->token)
->get("https://api.github.com/orgs/{$orgName}/members/{$githubUser->getNickname()}");
if ($membership->status() !== 204) {
Auth::logout();
return redirect('/login')->withErrors(['github' => 'Вход разрешён только для членов организации']);
} מה לעשות בנוגע לשגיאות API של GitHub?
ה-API של GitHub עשוי להיות לא זמין או להחזיר שגיאה. אנו מטמיעים מנגנוני לוגינג וגיבוי: אם הבקשה נכשלת, המשתמש רואה הודעה ברורה, ואנחנו מקבלים התראה. בנוסף, אנו מגדירים ניסיונות חוזרים עם backoff אקספוננציאלי.
מה כלול באינטגרציה
- יצירה והגדרה של OAuth App
- אינטגרציה של Laravel Socialite עם scopes מוגדרים
- יישום controllers וטיפול בשגיאות
- בדיקת כל התרחישים (דוא"ל פרטי, שלילת גישה, התחברות חוזרת)
- תיעוד פריסה ושבועיים של תמיכה לאחר המסירה
תהליך העבודה לאינטגרציה
- ניתוח — קביעת דרישות: צורך בסינון ארגוני, אילו נתוני פרופיל לשמור.
- עיצוב — יצירת OAuth App, הגדרת סביבה.
- יישום — חיבור Socialite, כתיבת controllers וטיפול בשגיאות.
- בדיקות — בדיקת תרחישים: דוא"ל פרטי, שלילת גישה, התחברות חוזרת.
- פריסה — העלאה לייצור, עדכון callback URL.
| פרמטר | GitHub OAuth | דוא"ל+סיסמה |
|---|---|---|
| זמן התחברות ראשון | ~5 שניות | ~60 שניות (פי 12 יותר) |
| מספר שגיאות קלט | 0 | ~30% מהמשתמשים |
| אבטחה | OAuth tokens | ניהול סיסמאות |
| אמון משתמשים | גבוה (95% בוחרים התחברות חברתית) | בינוני |
| השוואת יישום | Socialite | OAuth מותאם אישית |
|---|---|---|
| קוד | 15 שורות | 100+ שורות |
| זמן פיתוח | שעה | 1-2 ימים |
| סיכון לשגיאות | נמוך | גבוה |
| תמיכה בעדכונים | אוטומטית | ידנית |
ציר זמן
האינטגרציה אורכת 1-2 ימי עבודה. לאורך השנים, יישמנו GitHub OAuth בעשרות פרויקטים ומבטיחים אימות יציב. צרו קשר לייעוץ — נעריך את הפרויקט שלכם ונציע את הפתרון הטוב ביותר.







