Skip to content

Instantly share code, notes, and snippets.

View DenoGeek's full-sized avatar

Dennis Kariuki DenoGeek

View GitHub Profile
@DenoGeek
DenoGeek / token.go
Created February 4, 2019 07:15
Golang Token interceptor
//in main .go just chain the methods like this your namespace may change depending on your app organisation
router.GET("/api/user", middlewares.JwtAuth(A.GetUser))
router.GET("/api/user", middlewares.JwtAuth(A.GetApplications))
//Have a the above JWTAuth taking in a handler and returning one after checks. NOTICE ON SUCCESSFUL CHECKS I NOW CALL THE HANDLER
//THAT WAS PASSED
func JwtAuth(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@DenoGeek
DenoGeek / chat.html
Created February 4, 2019 13:37
The chatter box
<style type="text/css">
.chatbox {
position: fixed;
bottom: 0;
right: 30px;
width: 300px;
height: 400px;
background-color: #fff;
font-family: 'Lato', sans-serif;
$('.chatbox').css({
'position' : 'fixed',
'bottom' : '0',
'right' : '30px',
'width' : '300px',
'height' : '400px',
'background-color' : '#fff',
'font-family' : 'Lato, sans-serif',
'-webkit-transition' : 'all 600ms cubic-bezier(0.19, 1, 0.22, 1)',
'transition' : 'all 600ms cubic-bezier(0.19, 1, 0.22, 1)',
<div class="chatbox chatbox--tray chatbox--empty">
<div class="chatbox__title">
<h5><a href="#">Customer Service</a></h5>
<button class="chatbox__title__tray">
<span></span>
</button>
<button class="chatbox__title__close">
<span>
<svg viewBox="0 0 12 12" width="12px" height="12px">
<line stroke="#FFFFFF" x1="11.75" y1="0.25" x2="0.25" y2="11.75"></line>
@DenoGeek
DenoGeek / query.php
Created April 3, 2019 22:44
crazy query
$orders = Order::join('assignments', 'orders.active_assignment', '=', 'assignments.id');
$orders->join('users', 'assignments.user_id', '=', 'users.id');
$orders->leftJoin('bargains', 'bargains.order_id', '=', 'orders.id');
$orders->doesntHave('payment');
$orders->where('orders.status',4);
$orders->select(['orders.id','bargains.order_id','users.id as user_id','orders.order_no','orders.salary','users.name',DB::raw('SUM(bargains.amount) As bargains_sum, (SUM(bargains.amount)+orders.salary) as total')]);
$orders->groupBy(['orders.id']);
$result = $orders->get();
/*
@DenoGeek
DenoGeek / src_match.php
Last active April 22, 2019 16:52
Src pregmatch
$iframe_string = '<iframe width="100%" height="315" src="https://www.youtube.com/embed/-XC6r9SIQfo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
preg_match('/src="([^"]+)"/', $iframe_string, $match);
/*Output all the matches by the expression*/
print_r($match);
/*Get the index of the array*/
$url = $match[1];
class MobileAuthSerializer(serializers.Serializer):
phone_number=serializers.CharField(required=True)
def save(self):
#Retrieve the user with this mobile number or create a user instance with that phone number
try:
user=User.objects.get(mobile=self.validated_data['phone_number'])
except Exception as e:
user=User.objects.create(mobile=self.validated_data['phone_number'],email=self.validated_data['phone_number'])
#Generate the users token and send it out
class MobileToken(models.Model):
verification_code=models.CharField(max_length=225)
user=models.ForeignKey(settings.AUTH_USER_MODEL, related_name="mobile_tokens",on_delete=models.CASCADE)
phone_number=models.CharField(max_length=225)
is_used=models.BooleanField(default=False)
created = models.DateTimeField("Created on", auto_now_add=True)
class MobileAuthView(generics.GenericAPIView):
permission_classes = ()
authentication_classes = ()
serializer_class=MobileAuthSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
class MobileTokenBaseSerializer(serializers.Serializer):
verification_code=serializers.CharField()
def validate_verification_code(self,value):
if not MobileToken.objects.filter(verification_code=value).exists():
raise serializers.ValidationError("The token provided does not exist")
elif MobileToken.objects.filter(verification_code=value).first().is_used:
raise serializers.ValidationError("The token provided is used")
return value