Apple ile bir login girişi yaptırmaya çalışıyorum. Fakat bu apple dan jwt bana dışarıdan gelecek bu jwt'yi bulamadım. Test etmek için ek olarak kod da bir hata var mı size sormak istedim deneyemediğim için 😃
public function appleAuthToken(Request $request)
{
$request->validate([
'token' => 'required',
]);
$applePublicKeys = Http::get('https://appleid.apple.com/auth/keys')->json();
$jwt = $request->input('token');
$header = json_decode(base64_decode(explode('.', $jwt)[0]), true);
$kid = $header['kid'];
$publicKey = null;
foreach ($applePublicKeys['keys'] as $key) {
if ($key['kid'] === $kid) {
$publicKey = JWK::parseKey($key);
break;
}
}
if (!$publicKey) {
return response()->json(["status" => 401, "message" => 'Invalid Apple token'], 401);
}
try {
$appleUser = JWT::decode($jwt, $publicKey, ['RS256']);
$appleUserId = $appleUser->sub;
$appleUserEmail = $appleUser->email ?? null;
if (!$appleUserEmail) {
return response()->json(["status" => 401, "message" => 'Email not provided by Apple'], 401);
}
$user = User::where('email', $appleUserEmail)->first();
if (!$user) {
$user = User::create([
'name' => $appleUser->name ?? 'Unknown',
'reference_code' => $this->generateReferenceCode(),
'email' => $appleUserEmail,
'apple_user_id' => $appleUserId,
'password' => Hash::make(uniqid()),
]);
} else {
if ($user->apple_user_id === null) {
$user->apple_user_id = $appleUserId;
$user->save();
}
}
$token = $user->createToken('apple-auth-token')->plainTextToken;
return response()->json(['message' => 'Success', 'data' => $user, 'token' => $token]);
} catch (\Exception $e) {
return response()->json(["status" => 401, "message" => 'Invalid Apple token'], 401);
}
}