danog / MadelineProto

Async PHP client API for the telegram MTProto protocol
https://docs.madelineproto.xyz
GNU Affero General Public License v3.0
2.85k stars 661 forks source link

I'm not waiting for the code! Please call the phoneLogin method first though submitting correct code. #1299

Closed Meeralamin closed 1 year ago

Meeralamin commented 1 year ago

203368685-587a0d03-02f4-4f82-92cd-125ccd57feb4

I am facing this issue though I am submitting the correct code on laravel.

public function loginCodeCheck(Request $request) {

try {
    $user = User::find(auth()->id());
    $telegramSession = $user->telegramSession;
    $madelineProto = $this->factory->get($telegramSession);

    $secret_code = $request->code;
    $madelineProto->completePhoneLogin($secret_code);

    return redirect()->route('user.add.product')->with('success', 'You have successfully logged in to Telegram, Now you can add or edit product.');
} catch (\Throwable $th) {
    return redirect()->route('user.telegram.login')->with('error', $th->getMessage());
}

}

public function loginCheckAjax(Request $request) { $validator = Validator::make($request->all(), [ 'phone' => 'required', ]);

if (!$validator->passes()) {
    return response()->json(['error' => $validator->errors()->all()]);
}
try {
    $user = User::find(auth()->id());

    TelegramSession::where('user_id', $user->id)->delete();
    File::delete(File::glob(storage_path("app/telegram/" . $user->id . "*")));

    $telegramSession = $user->telegramSession()->create([
        'session_file' => "{$user->id}.madeline"
    ]);
    $madelineProto = $this->factory->get($telegramSession);

    $phone = $request->phone;
    $madelineProto->phoneLogin($phone);

    return response(['codesent' => 1], 200);

} catch (\Throwable $th) {
    $user = User::find(auth()->id());
    if ($user->telegramSession) {
        $session = $user->telegramSession->orderBy('id', 'desc')->first();
        if ($session != null) {
            File::delete(File::glob(storage_path("app/telegram/" . $session->session_file . "*")));
            $user->telegramSession->delete();
        }
    }
    if ($th->getMessage() == 'PHONE_NUMBER_INVALID') {
        return response()->json(['error' => 'You phone number is not registered or is banned on Telegram. Use another phone number.']);
    } else {
        return response()->json(['error' => $th->getMessage()]);
    }
}

}

This is the function. I didn't try without mysql, I need always multisession, I am currently using telegram_session table in database to save session and file being save in storage/app/telegram folder

danog commented 1 year ago

What do the logs say?

danog commented 1 year ago

Should be fixed in the latest beta96