Wednesday, November 20, 2024

Choose between Laravel Sanctum Cookie-Based Vs Token Based and Laravel Sanctum Token Based vs Laravel Passport

 Laravel Sanctum is a package that offers both cookie-based and token-based authentication for Laravel applications: 

Cookie-based authentication

This is a good choice for traditional web applications, especially when the front-end and API are on the same domain. Sanctum uses Laravel's built-in cookie-based session authentication services, which provide CSRF protection and session authentication. This approach is ideal for single-page applications (SPAs) that need to communicate with a Laravel API. 

Token-based authentication

This is a more secure and scalable option than cookie-based authentication. Sanctum can be used to generate and manage API tokens, authenticate users, and protect API routes. This approach is more suitable for applications that need fast authorization or more interaction between the client and server. 

Here are some other considerations when choosing between cookie-based and token-based authentication:

Domain

If the front-end and API are on different domains, cookie-based authentication may not be suitable.

Third-party API consumption

If the application involves third-party API consumption, you'll need to decide between Passport and Sanctum for API token authentication.

OAuth2

If the application requires the full range of OAuth2 features, Passport is the right choice. 

Friday, March 4, 2022

Golang: Dynamic Array sample

sample code of dynamic array in Golang.



 // You can edit this code!

// Click here and start typing.

package main


import "fmt"


type Arr struct {

arr []string

}


type SError struct {

code    int32

message string

}


func main() {

newArr := &Arr{arr: []string{"a", "b", "c", "d", "e", "f", "g"}}


fmt.Println(newArr.size())


str, err := newArr.get(8)

if err != nil {

fmt.Println(err)

} else {

fmt.Printf("item value = %s", str)

}


newArr.set(10, "asssss")


err = newArr.removeAt(10)

if err != nil {

fmt.Println(err)

} else {

fmt.Printf("removed with index :  %v", newArr.arr)

}


err = newArr.remove("fasd")

if err != nil {

fmt.Println(err)

} else {

fmt.Printf("remove with string: %v", newArr.arr)

}

}


func (arr *Arr) get(i int) (string, *SError) {

if i > arr.size()-1 || i < 0 {

return "", &SError{code: 5, message: fmt.Sprintf("Error accesing index %d from array.", i)}

}


return arr.arr[i], nil

}


func (arr *Arr) set(i int, v string) *SError {

if i > arr.size()-1 || i < 0 {

return &SError{code: 4, message: fmt.Sprintf("%d not exists in the array.", i)}

}


arr.arr[i] = v

return nil

}


func (arr *Arr) add(v string) {

arr.arr = append(arr.arr, v)

}


func (arr *Arr) removeAt(i int) *SError {

if i > arr.size()-1 || i < 0 {

return &SError{code: 3, message: fmt.Sprintf("Error removing index %d from array.", i)}

}

arr.arr = append(arr.arr[:i], arr.arr[i+1:arr.size()]...)

return nil

}


func (arr *Arr) remove(v string) *SError {


if arr.size() == 0 {

return &SError{code: 1, message: "Array is empty"}

}


// find the index

for k, val := range arr.arr {

if val == v {

// and removeAt

arr.removeAt(k)

return nil

}

}


return &SError{code: 2, message: fmt.Sprintf("Error removing %s from array.", v)}

}


func (arr *Arr) size() int {

return len(arr.arr)

}


Tuesday, October 20, 2020

to copy a file from a docker container to host

shell>  docker cp <containerId>:/file/path/within/container /host/path/target


where to get your docker container id? 


shell> docker ps


and you will get to see similar output in your command prompt as following:


CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                    NAMES

fff29505492e        localstack_redis:latest        "docker-entrypoint.s…"   2 weeks ago         Up 8 hours          0.0.0.0:6379->6379/tcp   gifted_wozniak

2e99dabb5692        localstack_mysql:latest        "docker-entrypoint.s…"   2 weeks ago         Up 8 hours          0.0.0.0:3306->3306/tcp   vibrant_chandrasekhar

7438b2ef7cbb        localstack_prom2teams:latest   "sh prom2teams_start…"   2 weeks ago         Up 8 hours          8089/tcp                 cranky_merkle


Case : to copy a file in your docker location such as /tmp/target.txt from localstack_mysql:latest. 

shell> docker cp 2e99dabb5692:/tmp/target.txt c:/tmp/

Tuesday, June 16, 2020

To Delete all data from Kafka Topic - in a manual way

to delete the data inside the topic:
Steps:
  1. stop the Kafka server & zookeeper
  2. go into this folder - > gerrit\backend\docker\zk-single-kafka-single\kafka1\data
  3. and delete all the "XXX" topic folder 
  4. start the kafka & zookeeper 

Friday, May 22, 2020

Java: To read file from JAR file classpath

if the *.xml file and .config or even some other file are located in different jars, make sure you use

resource.getInputStream()

NOT

resource.getFile()

when you are loading a file from inside a jar.

Sunday, October 6, 2019

Install Maven in Mac OSX

to install maven in Mac OSX, the easiest way is to use Homebrew

OR using the following steps:

  • Download maven from https://maven.apache.org
  • unzip the apache maven zip file into a location when you fix /User/XXX/Desktop/dev/apache-maven-X.X.X
  • then you would need to add the maven folder into your user's MacOSX path using vi or other text editor tools
            shell> vi ~/.bash_profile

file content

export M2_HOME=/path/to/your/maven/folder .  (e.g: /User/XXX/Desktop/dev/apache-maven-X.X.X)
export PATH=$PATH:$M2_HOME/bin

  • save the file by press "ESC" button, then type this command -> ":wq!"
  • you would need to refresh the system PATH using following command
            shell> source ~/.bash_profile
  • after that, type shell> mvn --version








Wednesday, December 12, 2018

Spring: Clean up CacheManager Java code


If you had use @Cacheable for your java method, and would like clear the cache on run time.
AND If you are able to execute a groovy script, following solution is for you

Groovy Script

import org.springframework.cache.CacheManager;
cacheManager = spring.getBean("cacheManager");

for(String name:cacheManager.getCacheNames()){
            cacheManager.getCache(name).clear();
}