Skip to content

Instantly share code, notes, and snippets.

View Kaival-Patel's full-sized avatar
🦏
Flutterrrrrr

Kaival Patel Kaival-Patel

🦏
Flutterrrrrr
  • Devkrest Technologies
  • Ahmedabad
  • 16:04 (UTC -12:00)
View GitHub Profile
import '/src/config/dio_config.dart';
import '/src/models/api_response_model.dart';
import '/src/models/user_model.dart';
import '/src/utils/constants/constants.dart';
class UserNetworkService {
Future<ApiResponse<List<UserModel>>> getListOfUsers() async {
try {
var res = await Api().get();
var apiRes = ApiResponse<List<UserModel>>.fromListJson(
@Kaival-Patel
Kaival-Patel / user_listing_controller.dart
Created May 21, 2023 14:53
Getx Controller for fetching the users listing
import 'package:get/get.dart';
import '/src/backend/api/user_nw_service.dart';
import '/src/models/user_model.dart';
class UserListController extends GetxController
with StateMixin<List<UserModel>> {
@override
void onInit() {
super.onInit();
fetchUsers();
}
@Kaival-Patel
Kaival-Patel / getx_state_demo.dart
Created May 21, 2023 14:39
UI part for the GetX State Mixin
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '/src/modules/home/user_display_controller.dart';
class GetXStateMixinDemo extends StatelessWidget {
GetXStateMixinDemo({super.key});
var c = Get.put(UserListController());
@override
@Kaival-Patel
Kaival-Patel / api_response.json
Created May 21, 2023 13:41
Api Response in Json
{
"s":1,
"m":"Success",
"r":[
{
"gender": "male",
"name": {
"title": "Mr",
"first": "Oswaldo",
"last": "Hernádez"
@Kaival-Patel
Kaival-Patel / api_generic_model.dart
Created January 27, 2023 09:41
Api Generic Response Model
import 'dart:convert';
String apiResponseToJson(ApiResponse data) => json.encode(data.toJson());
class ApiResponse<T> {
ApiResponse({
this.s = 0,
this.m = "",
this.r,
@Kaival-Patel
Kaival-Patel / AuthorizationController.swift
Last active January 20, 2023 10:04
Authorization Controller Implementation for Error and success handling callbacks
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
let logger = Logger()
switch authorization.credential {
case let credentialRegistration as ASAuthorizationPlatformPublicKeyCredentialRegistration:
logger.log("A new passkey was registered: \(credentialRegistration)")
// Verify the attestationObject and clientDataJSON with your service.
// The attestationObject contains the user's new public key to store and use for subsequent sign-ins.
// let attestationObject = credentialRegistration.rawAttestationObject
// let clientDataJSON = credentialRegistration.rawClientDataJSON
@Kaival-Patel
Kaival-Patel / Registration.swift
Created January 20, 2023 09:47
Registration Implementation for Passkeys
func signUpWith(userName: String, anchor: ASPresentationAnchor) {
self.authenticationAnchor = anchor
let publicKeyCredentialProvider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: domain)
// Fetch the challenge from the server. The challenge needs to be unique for each request.
// The userID is the identifier for the user's account.
let challenge = Data()
let userID = Data(UUID().uuidString.utf8)
let registrationRequest = publicKeyCredentialProvider.createCredentialRegistrationRequest(challenge: challenge,
@Kaival-Patel
Kaival-Patel / SignInMethod.swift
Created January 20, 2023 09:35
SignInMethod Implementation for Passkeys
func signInWith(anchor: ASPresentationAnchor, preferImmediatelyAvailableCredentials: Bool) {
self.authenticationAnchor = anchor
let publicKeyCredentialProvider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: domain)
// Fetch the challenge from the server. The challenge needs to be unique for each request.
let challenge = Data()
let assertionRequest = publicKeyCredentialProvider.createCredentialAssertionRequest(challenge: challenge)
// Also allow the user to use a saved password, if they have one.
@Kaival-Patel
Kaival-Patel / AccountManager.swift
Last active January 20, 2023 09:31
Passkeys Demonstration (Signup Process)
import AuthenticationServices
import Foundation
import os
extension NSNotification.Name {
static let UserSignedIn = Notification.Name("UserSignedInNotification")
static let ModalSignInSheetCanceled = Notification.Name("ModalSignInSheetCanceledNotification")
}
class AccountManager: NSObject, ASAuthorizationControllerPresentationContextProviding, ASAuthorizationControllerDelegate {
//To setup backend side, https://quickbirdstudios.com/blog/ios-passkeys/
//Define your connected domain
@Kaival-Patel
Kaival-Patel / dio_config.dart
Created January 10, 2023 09:35
Dio Configuration for working with api calls in flutter
import 'dart:io';
import 'package:dio/dio.dart';
class Api {
final dio = createDio();
String _token = "";
String _apiKey = "";
Api._internal();